I have a superclass called Transaction that has a property named TransactionId. This property must be set to some value at the constructor of all subclasses.
public class SubTransaction : Transaction
{
public SubTransaction() : base()
{
this.TransactionId = "IdTransaction";
}
}
I have lots of this kind of subclasses.
What I want to do: using reflection load the assembly of these SubTransactions and get the Id set by each one. Is that possible?
By the way, I can’t instantiate the objects because I don’t have all information that I need. It is completely impossible for me to do that.
Well you could try reading the IL of the body of the constructor, but I really wouldn’t suggest it.
I wonder whether it might not be better to decorate each class with an attribute, and read that instead…
The base class could load the transaction ID in the same way, if it still needed to.
Alternatively, each class could declare a constant field, always with the same name:
That should be easy to read with reflection. It’s ugly, but you’re basically in an ugly situation…