问题描述
您能否在创建时不指定实例的情况下创建实例方法的委托?换句话说,您能否创建一个静态"委托,将调用该方法的实例作为其第一个参数?
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?
我知道我可以使用 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
时,可以使用Delegate.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.
正如 Jon Skeet 所指出的,如果方法在引用类型上是非虚拟的,您可以简单地应用相同的方法来创建实例方法的开放委托.决定在虚拟方法上调用哪个方法很棘手,所以这不是那么简单,而且值类型看起来根本不起作用.
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:
如果实例方法属于值类型(这适用于引用类型),则以 null
作为目标对象调用 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).
几年后的一些跟进:导致func42(CultureInfo.InvariantCulture);
返回"-201040128" 在我的示例中,而不是 "42"
是内存损坏,可能允许远程执行代码 (cve-2010-1898);这已在 2010 年的 ms10-060 安全更新.当前框架正确打印 42!这并没有使回答这个问题变得更容易,但解释了示例中特别奇怪的行为.
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:
- ToString() 是继承自
object
但在 Int32
中被覆盖的虚方法. int
是一个值类型,当涉及到值类型和实例方法时,Delegate.CreateDelegate()
有一些奇怪的规则——基本上第一个有效参数变成了ref int
而不是 int
- ToString() is a virtual method inherited from
object
but overridden in Int32
.
int
is a value type, and there are weird rules with Delegate.CreateDelegate()
when it comes to value types and instance methods - basically the first effective parameter becomes ref int
rather than int
但是,下面是 String.ToUpper
的示例,它不存在这些问题:
However, here's an example for String.ToUpper
, which doesn't have either of those problems:
如果这对你来说足够好,那太好了......如果你真的想要 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:
这篇关于“不客气".NET 中的实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!