views:

67

answers:

1

I've gotten myself in a pickle, and could use the help of a guru...

I have a Journal that records entries for different types:

Journal(Of ParentT) - Parent could be Customer, Address, other classes

The constructor of the Journal requires knowledge of the Type parameter:

Public Sub New(Parent as ParentT)

In my consuming form, I take a Journal in the constructor:

Public Sub DisplayForm(Journal as object)

At this point, I cannot determine what type the Journal is for. I have looked at using Reflection with the MethodInfo > MakeGenericMethod, DynamicMethod, delegates, etc, but haven't found a workable solution.

I am willing to consider most any option at this point...

+1  A: 

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<ParentT> 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<int>();
 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 
{
    ...
}
Ani
Wow - I spent hours looking for this! Thanks.
grefly
@grefly: Great. Are you sure you can't change the design to not require reflection though?
Ani
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