Determining a Generic Type at Runtime in Non-Generic Class?

I may have misunderstood the question, but if I understand correctly Journal is in fact a generic class with generic parameter ParentT it is only that the reference to a Journal(); Console. WriteLine(obj.GetType(). GetGenericArguments().First().ToString()) Output: System.

Int32 In your case, you might want something like: Type journalGenericType = myJournal.GetType(). GetGenericArguments().First(); if (journalGenericType == typeof(Customer)) { ... } else { ... }.

I may have misunderstood the question, but if I understand correctly, Journal is in fact a generic class with generic parameter ParentT; it is only that the reference to a Journal instance is of the non-generic System. Object type. In this case, the following method should work fine: System.Type.

GetGenericArguments Sorry that this code is in C#, but: object obj = new List(); Console. WriteLine(obj.GetType(). GetGenericArguments().First().ToString()); Output: System.

Int32 In your case, you might want something like: Type journalGenericType = myJournal.GetType(). GetGenericArguments().First(); if (journalGenericType == typeof(Customer)) { ... } else { ... }.

Wow - I spent hours looking for this! Thanks. – grefly Aug 14 '10 at 6:17 @grefly: Great.

Are you sure you can't change the design to not require reflection though? – Ani Aug 14 '10 at 16:04 I have to make a decision in IoC based on type of Journal - (I did not favor Convention over Configuration! ) - so what would that design change look like?

I am open to suggestions/recommendataions... – grefly Aug 16 '10 at 2:26.

I know I'm a bit late to this party, but I thought I'd weigh in. There are a number of non-reflection-based approached you could use here. This DisplayMethod call that you pass the journal to isn't the constructor of your form (otherwise it'd be called New) so I assume that it is a method that figures out which form to load to display the journal.

If so, you could simply add the generic parameter to this method call like so: Public Sub DisplayForm(Of ParentT)(Journal As Journal(Of ParentT)) Dim JournalParentType = GetType(ParentT) '... End Sub Since you're using IoC you could even go one step further. Something like this: Public Sub DisplayForm(Of ParentT)(Journal As Journal(Of ParentT)) Dim form = ioc. Resolve(Of IForm(Of ParentT)) form.

DataSource = Journal. Parent form.Show() End Sub Of course you would need to define your own IForm(Of T) interface to make this work - but now there is no reflection required. Another approach would be to have a Journal base class of Journal(Of ParentT) and have a ParentType property on Journal.

Alternatively you could have an IJournal interface that does the same thing.

Another approach would be to have a Journal base class of Journal(Of ParentT) and have a ParentType property on Journal. Alternatively you could have an IJournal interface that does the same thing.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions