Created
April 23, 2015 08:16
-
-
Save leppie/dc6d4d3bbb1c6fc476db to your computer and use it in GitHub Desktop.
Delegate conversion via reflection with DynamicMethod issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection.Emit; | |
class Program | |
{ | |
delegate object FuncObjectObject(Object o); | |
static void Main(string[] args) | |
{ | |
var dm = new DynamicMethod("", typeof(object), new [] { typeof(object) }); | |
var ilgen = dm.GetILGenerator(); | |
ilgen.Emit(OpCodes.Ldarg_0); | |
ilgen.Emit(OpCodes.Ret); | |
var dmd = (Func<object, object>)dm.CreateDelegate(typeof(Func<object, object>)); | |
Console.WriteLine(dmd(1)); // works | |
// now do the same, but with reflection | |
var rd = (FuncObjectObject) Delegate.CreateDelegate(typeof(FuncObjectObject), dmd.Target, dmd.Method); // kaboom! | |
// wrap the delegate | |
var sdm = new FuncObjectObject(dmd); | |
Console.WriteLine(sdm(1)); // works | |
var srd = (FuncObjectObject)Delegate.CreateDelegate(typeof(FuncObjectObject), sdm.Target, sdm.Method); // works | |
Console.WriteLine(srd(1)); | |
// lets try something else | |
var ad = (FuncObjectObject) Activator.CreateInstance(typeof(FuncObjectObject), dmd); // kaboom! | |
} | |
} |
For the rest: ultimately, dynamic methods have restrictions and limitations (but also: have some nice extra features, like the ability to spoof accessibility to a type outside your direct control). Some things don't work on them. If you need it to behave as a full method, you might need to use TypeBuilder
and MethodBuilder
to create a (presumably) static
method on a dummy Type
via CreateType()
etc. Much more work.
yay, found a workaround!
var ad = (FuncObjectObject)Activator.CreateInstance(typeof(FuncObjectObject),
dmd, dmd.GetType().GetMethod("Invoke").MethodHandle.GetFunctionPointer()); // works!
Console.WriteLine(ad(1)); // works!
Thanks @mgravell for hinting towards to right direction 😄
heh; nice
Simpler:
var ad = (FuncObjectObject)Delegate.CreateDelegate(typeof(FuncObjectObject), dmd, "Invoke");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The constructor for delegates actually takes two parameters; the C# compiler lies and splits out the target and method for you; the actual usage would be:
However: it doesn't work with dynamic methods.