use*_*865 5 c# reflection mono delegates
嗨,我正在尝试在Mono 2.8.2中创建一个信使 - Unity3d使用的子集.我认为当使用"subscribe"属性修饰信号时,创建一个帮助自动订阅信使方法是很好的.
我一直在摸不着头脑,并且已经阅读了许多其他相关的堆栈问题而没有解决我的问题.坦率地说,我不知道我做错了什么,或者这是否是Mono中的一个错误.
foreach (var methodInfo in methods)
{
var attr = methodInfo.GetAttribute<SubscribeAttribute>();
if (attr == null)
continue;
var parmas = methodInfo.GetParameters();
if (parmas.Length != 1)
{
Debug.LogError("Subscription aborted. Invalid paramters.");
continue;
}
var type = parmas[0].ParameterType;
// Crashes here
// ArgumentException: method argument length mismatch
// I have tried many combinations..
// Direct typing of the message type and dynamic typing
var action = (Action<object>)Delegate.CreateDelegate(typeof(Action<object>), methodInfo);
// also does not work
// var dt = Expression.GetActionType(parmas.Select(o => o.ParameterType).ToArray());
// var action = Delegate.CreateDelegate(dt, methodInfo);
Subscribe(type, action, instance);
}
Run Code Online (Sandbox Code Playgroud)
任何建议或工作将不胜感激.
编辑 方法签名如下所示:
[Subscribe]
void OnMessage(object message){
// Hello World
}
Run Code Online (Sandbox Code Playgroud)
虽然,它最初是......
[Subscribe]
void OnTestMessage(TestMessage message){
// Hello World
}
Run Code Online (Sandbox Code Playgroud)
这是一种非静态方法,您没有提供目标对象.因此,Delegate.CreateDelegate将创建一个带有显式this参数的"开放委托" .
由于所需的this参数,它不再匹配签名.