Joh*_*n K 7 .net c# delegates typechecking
在C#代码中,如何检查给定方法是否可以由特定委托类型表示?
我首先根据我的类型知识尝试了一些东西:
// The delegate to test against.
void TargetDelegate(string msg);
// and...
var methodInfo = Type.GetMethod(..); // obtain the MethodInfo instance.
// try to test it
typeof(TargetDelegate).IsAssignableFrom(methodInfo.GetType());
Run Code Online (Sandbox Code Playgroud)
但这只涉及类型而不是方法 - 它总是错误的.
我倾向于相信答案在于Delegate
Type,但我现在只是在FCL四处游荡.任何帮助,将不胜感激.
我试试:
Delegate.CreateDelegate(typeof(TargetDelegate), methodInfo, false) != null
Run Code Online (Sandbox Code Playgroud)
这将尝试创建委托并在失败时返回null.如果它返回null,则意味着无法创建委托.如果它返回任何其他内容,则委托必须正常.
归档时间: |
|
查看次数: |
1477 次 |
最近记录: |