如何在c#中检查EventInfo和MethodInfo之间的签名兼容性? publicstaticboolchecksignature(EventInfo-EventInfo,MethodInfo-MethodInfo) { //支票签名 } 公共类单项法 { 公共部门目标; 公共字符串方法名; 公共静态委托CreateDelegate(Monmethod Monmethod) { if(monoMethod.target==null | | string.IsNullOrEmpty(monoMethod.methodName)) { 返回null; } //在CreateDelegate之前检查兼容性 //…如果为false,则返回null 返回Delegate.CreateDelegate(typeof(T),monoMethod.target,monoMethod.methodName); } }
我想检查如何在c#中检查EventInfo和MethodInfo之间的签名兼容性? publicstaticboolchecksignature(EventInfo-EventInfo,MethodInfo-MethodInfo) { //支票签名 } 公共类单项法 { 公共部门目标; 公共字符串方法名; 公共静态委托CreateDelegate(Monmethod Monmethod) { if(monoMethod.target==null | | string.IsNullOrEmpty(monoMethod.methodName)) { 返回null; } //在CreateDelegate之前检查兼容性 //…如果为false,则返回null 返回Delegate.CreateDelegate(typeof(T),monoMethod.target,monoMethod.methodName); } },c#,reflection,C#,Reflection,我想检查事件信息和方法信息之间的签名(也称返回类型和参数信息),然后再从该方法信息创建委托。 我可以从MethodInfo获取ReturnType和ParameterInfo,但不能从EventInfo获取。 是否仍要实现?对于EventInfo,EventHandlerType将是一个代理 通过反射查看Invoke方法以查找签名 然后简单地与MethodInfo进行比较,我不太确定您想要做什么,但是EventInfo.GetRaiseMethod()。也许你想比较一下这个。@Christian
事件信息
和方法信息
之间的签名(也称返回类型和参数信息),然后再从该方法信息创建委托。
我可以从MethodInfo
获取ReturnType和ParameterInfo,但不能从EventInfo
获取。
是否仍要实现?对于
EventInfo
,EventHandlerType
将是一个代理
通过反射查看Invoke
方法以查找签名
然后简单地与
MethodInfo
进行比较,我不太确定您想要做什么,但是EventInfo.GetRaiseMethod()
。也许你想比较一下这个。@Christian.K不。我想知道是否可以在运行时将此方法添加到目标eventHandler。确定,在我放弃之前的最后一个;-)隐马尔可夫模型。。。你看过我链接的答案了吗?:-)我想你只需要“什么都不做”,但当签名不匹配时,调用不抛出但返回null的。@Christian.K谢谢!我想它可能会扔出去P
public static bool CheckSignature (EventInfo eventInfo, MethodInfo methodInfo)
{
//check signature
}
public class MonoMethod
{
public Component target;
public string methodName;
public static Delegate CreateDelegate<T>(MonoMethod monoMethod)
{
if (monoMethod.target == null || string.IsNullOrEmpty(monoMethod.methodName))
{
return null;
}
//Check compatibility before CreateDelegate
//...if false, return null
return Delegate.CreateDelegate(typeof (T), monoMethod.target, monoMethod.methodName);
}
}