Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on?
For example, how can I construct the following delegate using reflection?
Func<int, string> = i=>i.ToString();
I'm aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called.
When you have the MethodInfo
of a particular static method, it is possible to construct a delegate using Delegate.CreateDelegate(delegateType, methodInfo)
, and all parameters of the static method remain free.
As Jon Skeet pointed out, you can simply apply the same to make an open delegate of an instance method if the method is non-virtual on a reference type. Deciding which method to call on a virtual method is tricky, so that's no so trivial, and value-types look like they don't work at all.
For value types, CreateDelegate
exhibits really weird behavior:
var func37 = (Func<CultureInfo,string>)(37.ToString);
var toStringMethod = typeof(int).GetMethod("ToString", BindingFlags.Instance | BindingFlags.Public, null, new Type[] {typeof(CultureInfo) }, null);
var func42 = (Func<CultureInfo,string>)Delegate.CreateDelegate(typeof(Func<CultureInfo,string>), 42, toStringMethod,true);
Console.WriteLine( object.ReferenceEquals(func37.Method,func42.Method)); //true
Console.WriteLine(func37.Target);//37
Console.WriteLine(func42.Target);//42
Console.WriteLine(func37(CultureInfo.InvariantCulture));//37
Console.WriteLine(func42(CultureInfo.InvariantCulture));//-201040128... WTF?
Calling CreateDelegate
with null
as the target object throws a binding exception if the instance method belonged to a value type (this works for reference types).
Some follow-up years later: The incorrectly-bound target that caused func42(CultureInfo.InvariantCulture);
to return "-201040128"
instead of "42"
in my example was memory corruption that could have allowed remote code execution (cve-2010-1898); this was fixed in 2010 in the ms10-060 security update. Current frameworks correctly print 42! That doesn't make answering this question any easier, but explains the particularly weird behavior in the example.
You've actually chosen a particularly tricky example, for two reasons:
object
but overridden inInt32
.int
is a value type, and there are weird rules withDelegate.CreateDelegate()
when it comes to value types and instance methods - basically the first effective parameter becomesref int
rather thanint
However, here's an example for
String.ToUpper
, which doesn't have either of those problems:If that's good enough for you, great... if you really want
int.ToString
, I'll have to try a bit harder :)Here's an example for a value type, using a new delegate type which takes its first parameter by reference:
You could use Lambdas to get a "somewhat" compiled static wrapper for your instance method.
The sample below isn't exactly blazingly fast, yet it should be significantly faster than any plain dynamic invoke.
The output
The code
I'm not sure, but may be Open delegates can help you.
Upd: Follow this link, if first one don't works.
The goog way maybe can be useing the "dynamic" type in .NET 4.0. However the Delegate need the instance (for non-static methods). The problems is more complex then lokks at first time because of polymorfism etc...