c# – 检查方法与给定Delegate的兼容性?
发布时间:2020-12-15 04:31:19 所属栏目:百科 来源:网络整理
导读:在C#代码中,如何检查给定方法是否可以由特定委托类型表示? 我首先根据我的类型知识尝试了一些东西: // The delegate to test against.void TargetDelegate(string msg);// and...var methodInfo = Type.GetMethod(..); // obtain the MethodInfo instance.
在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()); 但这只涉及类型而不是方法 – 它总是错误的. 我倾向于相信答案在于 解决方法
我试试:
Delegate.CreateDelegate(typeof(TargetDelegate),methodInfo,false) != null 这将尝试创建委托并在失败时返回null.如果它返回null,则意味着无法创建委托.如果它返回任何其他内容,则委托必须正常. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |