" Uncurrying"在.NET实例方法 [英] "Uncurrying" an instance method in .NET
问题描述
编辑: 突出困难,虚拟和/或价值型的方法的
您可以创建一个实例方法的委托没有在创建时指定的实例?换句话说,你可以创建一个静态的委托,它接受,因为它是第一个参数实例的方法应该算得上?
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();
我知道的事实,我可以用methodInfo.Invoke,但这是慢,不检查类型的正确性,直到它被调用。
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.
当你有的MethodInfo
的特定的静态的方法,可以构建使用委派代表。 CreateDelegate(delegateType,MethodInfo的)
,和静态方法的所有参数都保持自由。
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.
编辑:对于值类型, CreateDelegate
展品真是怪异的行为:
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?
调用 CreateDelegate
与空
为目标对象抛出的结合异常,如果实例方法属于值类型(这适用于引用类型)。
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).
推荐答案
您实际上已经选择了一个特别棘手的例子,有两个原因:
You've actually chosen a particularly tricky example, for two reasons:
- 的ToString()是继承自
对象
,但覆盖在的Int32
虚方法。 -
INT
是值类型,并且有奇怪的规则与Delegate.CreateDelegate()
,当涉及到值类型和实例方法 - 基本上是第一个有效参数成为REF INT
,而不是INT
- ToString() is a virtual method inherited from
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
不过,这里是 String.ToUpper
的例子,它不具有任何的这些问题:
However, here's an example for String.ToUpper
, which doesn't have either of those problems:
using System;
using System.Reflection;
class Test
{
static void Main()
{
MethodInfo method = typeof(string).GetMethod
("ToUpper", BindingFlags.Instance | BindingFlags.Public,
null, new Type[]{}, null);
Func<string, string> func = (Func<string, string>)
Delegate.CreateDelegate(typeof(Func<string, string>),
null,
method);
string x = func("hello");
Console.WriteLine(x);
}
}
如果这是对你不够好,伟大的...如果你真的想要 int.ToString
,我得试试有点困难:)
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:
using System;
using System.Reflection;
public struct Foo
{
readonly string value;
public Foo(string value)
{
this.value = value;
}
public string DemoMethod()
{
return value;
}
}
class Test
{
delegate TResult RefFunc<TArg, TResult>(ref TArg arg);
static void Main()
{
MethodInfo method = typeof(Foo).GetMethod
("DemoMethod", BindingFlags.Instance | BindingFlags.Public,
null, new Type[]{}, null);
RefFunc<Foo, string> func = (RefFunc<Foo, string>)
Delegate.CreateDelegate(typeof(RefFunc<Foo, string>),
null,
method);
Foo y = new Foo("hello");
string x = func(ref y);
Console.WriteLine(x);
}
}
这篇关于&QUOT; Uncurrying&QUOT;在.NET实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!