Kir*_*ril 5 c# reflection delegates dynamic
我想创建一个Delegate
反射方法,但Delegate.CreateDelegate
需要Type
指定委托.是否有可能动态创建一个匹配任何功能的'委托'?
这是一个简单的例子:
class Functions
{
public Functions()
{
}
public double GPZeroParam()
{
return 0.0;
}
public double GPOneParam(double paramOne)
{
return paramOne;
}
public double GPTwoParam(double paramOne, double paramTwo)
{
return paramOne+paramTwo;
}
}
static void Main(string[] args)
{
Dictionary<int, List<Delegate>> reflectedDelegates = new Dictionary<int, List<Delegate>>();
Functions fn = new Functions();
Type typeFn = fn.GetType();
MethodInfo[] methods = typeFn.GetMethods();
foreach (MethodInfo method in methods)
{
if (method.Name.StartsWith("GP"))
{
ParameterInfo[] pi = method.GetParameters();
if (!reflectedDelegates.ContainsKey(pi.Length))
{
reflectedDelegates.Add(pi.Length, new List<Delegate>());
}
// How can I define a delegate type for the reflected method at run time?
Delegate dlg = Delegate.CreateDelegate(typeof(???), fn, method);
reflectedDelegates[pi.Length].Add(dlg);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现最接近的是代码项目中的FastInvokeWrapper,但我仍然试图绕过它并且我不太明白如何GetMethodInvoker
将反射的方法绑定到FastInvokeHandler
.
归档时间: |
|
查看次数: |
1994 次 |
最近记录: |