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...

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
grefly
  • 1,181
  • 2
  • 12
  • 28

2 Answers2

1

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
  • 111,048
  • 26
  • 262
  • 307
  • Wow - I spent hours looking for this! Thanks. – grefly Aug 14 '10 at 06: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 02:26
0

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.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172