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...
I may have misunderstood the question, but if I understand correctly,
Journal
is in fact a generic class with generic parameterParentT
; it is only that the reference to aJournal<ParentT>
instance is of the non-genericSystem.Object
type. In this case, the following method should work fine:System.Type.GetGenericArguments
Sorry that this code is in C#, but:
Output:
In your case, you might want something like:
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 calledNew
) 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:
Since you're using IoC you could even go one step further. Something like this:
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 ofJournal(Of ParentT)
and have aParentType
property on Journal. Alternatively you could have anIJournal
interface that does the same thing.