Reputation: 3712
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.
Upvotes: 1
Views: 247
Reputation: 1502756
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...
[TransactionId("IdTransaction")]
public class SubTransaction : Transaction
{
}
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:
public class SubTransaction : Transaction
{
public const string ConstTransactionId = "IdTransaction";
public SubTransaction() : base()
{
this.TransactionId = ConstTransactionId;
}
}
That should be easy to read with reflection. It's ugly, but you're basically in an ugly situation...
Upvotes: 3
Reputation: 887867
This is not possible.
Instead, you can create a custom attribute that takes an ID and apply it to each subclass.
Upvotes: 0