|
|
||||||
|
#1
|
|
|
|
|
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 |
|
|
|
#2
|
|
|
|
|
"Florian Fordermaier" <fordi> wrote in message
news:5590 [..] > 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, 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. |
|
|
| Similar Threads | |
| How to retrieve a generic method with GetMethod (reflection) ? How to retrieve a generic method with GetMethod Code ------------------- #public static void Sort<T>( T[] array, Comparison<T> comparison [System.Reflection.MethodInfo]... |
|
| Trouble with Reflection.Emit, emitting bad MSIL please help. Ok, to make a long story short, I'm Emitting some MSIL from a c# application. I know that THIS is the MSIL I want to be... |
|
| Emitting a Generic Type that has a Generic Base Class Hey... I dont know if its possible but i'm trying to emit a class that would look like this The Base class commes from the code: public class BaseClass<T> { // Whatever... |
|
| Reflection.Emit and generic methods How do I use Reflection.Emit to produce code similar to this? public void bar<T>() { } public void foo<J>() { bar<J>(); } |
|
| Problem with System.Reflection.Emit-ed PInvoke method's arguments Okay, here's the deal. I'm trying to write a set of types that expose some API's exposed by a group of DLLs. Specifically, I'm writing some types that use PInvoke methods to... |
|
|
All times are GMT. The time now is 11:20 AM. | Privacy Policy
|