小编use*_*865的帖子

在Mono 2.8.2中从methodInfo创建委托

嗨,我正在尝试在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 …
Run Code Online (Sandbox Code Playgroud)

c# reflection mono delegates

5
推荐指数
1
解决办法
1823
查看次数

标签 统计

c# ×1

delegates ×1

mono ×1

reflection ×1