Zak*_*ley 40 .net c# reflection delegates methodinfo
我需要一个方法,该方法接受一个MethodInfo
表示具有任意签名的非泛型静态方法的实例,并返回绑定到该方法的委托,该委托稍后可以使用Delegate.DynamicInvoke
方法调用.我的第一次天真尝试看起来像这样:
using System;
using System.Reflection;
class Program
{
static void Main()
{
var method = CreateDelegate(typeof (Console).GetMethod("WriteLine", new[] {typeof (string)}));
method.DynamicInvoke("Hello world");
}
static Delegate CreateDelegate(MethodInfo method)
{
if (method == null)
{
throw new ArgumentNullException("method");
}
if (!method.IsStatic)
{
throw new ArgumentNullException("method", "The provided method is not static.");
}
if (method.ContainsGenericParameters)
{
throw new ArgumentException("The provided method contains unassigned generic type parameters.");
}
return method.CreateDelegate(typeof(Delegate)); // This does not work: System.ArgumentException: Type must derive from Delegate.
}
}
Run Code Online (Sandbox Code Playgroud)
我希望该MethodInfo.CreateDelegate
方法可以找出正确的委托类型本身.好吧,显然它不能.那么,如何创建一个System.Type
表示具有与提供的MethodInfo
实例匹配的签名的委托的实例?
Oks*_*mel 35
您可以使用System.Linq.Expressions.Expression.GetDelegateType方法:
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
class Program
{
static void Main()
{
var writeLine = CreateDelegate(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));
writeLine.DynamicInvoke("Hello world");
var readLine = CreateDelegate(typeof(Console).GetMethod("ReadLine", Type.EmptyTypes));
writeLine.DynamicInvoke(readLine.DynamicInvoke());
}
static Delegate CreateDelegate(MethodInfo method)
{
if (method == null)
{
throw new ArgumentNullException("method");
}
if (!method.IsStatic)
{
throw new ArgumentException("The provided method must be static.", "method");
}
if (method.IsGenericMethod)
{
throw new ArgumentException("The provided method must not be generic.", "method");
}
return method.CreateDelegate(Expression.GetDelegateType(
(from parameter in method.GetParameters() select parameter.ParameterType)
.Concat(new[] { method.ReturnType })
.ToArray()));
}
}
Run Code Online (Sandbox Code Playgroud)
在第二次检查中可能存在复制粘贴错误!method.IsStatic
- 您不应该ArgumentNullException
在那里使用.提供参数名作为参数是一种很好的风格ArgumentException
.
使用method.IsGenericMethod
,如果你想拒绝所有通用的方法和method.ContainsGenericParameters
如果您想拒绝无取代类型参数仅泛型方法.
归档时间: |
|
查看次数: |
20034 次 |
最近记录: |