枚举,委托字典集合,其中委托指向一个重载的方法 [英] Enum, Delegate Dictionary collection where delegate points to an overloaded method
问题描述
我有一个泛型集合(字典),存储枚举和委托。因此,如果用户提供了一个枚举值的方法作为参数,在收集相应的委托将得到执行。
I have a generic collection (Dictionary), which stores an enum and delegate. So if the user provides an enum value to a method as a parameter, the corresponding delegate in the collection will get executed.
这个方法,它的代表点,过载。当调用方法,我怎么能选择执行该方法的版本?
This method, which the delegate points to, is overloaded. When invoking the method, how can I choose which version of the method to execute?
感谢
推荐答案
委托只指向一个单一的过载 - 不以法团。通过你得到一个委托的方法的时候,你已经做重载决议。通常情况下,你可以在编译器中做到这一点:
The delegate only points to a single overload - not to the "method group". By the time you get a delegate to a method, you have already done overload resolution. Usually, you can do this just in the compiler:
using System;
class Foo {
int Bar() { return 1; }
void Bar(int a) { }
static void Main() {
Foo foo = new Foo();
Func<int> myDelegate = foo.Bar; // points to "int Bar()" version
}
}
如果该问题涉及到获得通过反射重载的方法 - 那么你可以在参数中指定的模式 Type.GetMethod()
(作为键入[]
)。这应该给你你想要的方式。
If the question relates to getting the overloaded method via reflection - then you can specify the pattern in the arguments to Type.GetMethod()
(as a Type[]
). This should give you the method you want.
要得到一个代表一个的MethodInfo
,使用 Delegate.CreateDelegate
。
To get a delegate from a MethodInfo
, use Delegate.CreateDelegate
.
这篇关于枚举,委托字典集合,其中委托指向一个重载的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!