Hello Group,
assume the following sample model:
interface IGenericMethods
{
void DoSomething<T>(T obj);
}
class GenericMethods
{
public void DoSomething<T>(T obj)
{
Console.WriteLine(obj.GetType().ToString());
}
}
I'm trying to dynamically emit a type(kind of a proxy) that implements
the interface IGenericMethods. In the dynamic DoSomething
implementation, the call shall just be delegated to an (existing)
instance(stored in a field) of class GenericMethods. The TypeBuilder
finishes without exceptions, but when calling the dynamic type's
DoSomething method I get an InvalidProgramException stating the CLR
has found an invalid program.
Comparing the IL of an handcrafted equivalent of what I'm trying to
generate with the actually generated IL shows no difference. My
question is: Is this not possible or are there some special things to
necessarily be aware of when emitting this kind of code.
Actually, this is just an example, the ultimate target is to generate
this kind of code for every kind of generic method, so I don't want to
generate code that just covers the example, but would be happy for
some advice on a general purpose procedure. I'm quite confused, since
everything >seems< to work fine, the IL is identical, but it's
throwing an exception when used.
Hope somebody can help me out here,
Thanks in advance
Florian
Ben Voigt [C++ MVP] - 01 Nov 2007 23:59 GMT
> Hello Group,
>
[quoted text clipped - 32 lines]
>
> Hope somebody can help me out here,
I remember having similar problems when I mixed up the TypeBuilder with the
Type returned from TypeBuilder.CreateType. You'd think they both would
cause the same code to be generated, but it didn't actually work that way.
As nearly as I can tell, you must always pass in a System::Type for imported
types, and TypeBuilder for types defined in the same assembly. Not sure if
this is your problem but it might be something to look for.
> Thanks in advance
> Florian