首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当尝试使用methodinfo.createdelegate创建委托时,我得到和错误:'ArgumentException:方法参数是不兼容的

当尝试使用methodinfo.createdelegate创建委托时,我得到和错误:'ArgumentException:方法参数是不兼容的
EN

Stack Overflow用户
提问于 2022-05-01 08:30:46
回答 1查看 158关注 0票数 1

我试图用Delegate.CreateDelegate创建一个委托,我得到了错误:

ArgumentException:方法参数是不兼容的System.Delegate.CreateDelegate (System.Type类型,System.Object firstArgument,System.Reflection.MethodInfo方法,System.Boolean throwOnBindFailure,System.Boolean allowClosed) (at <6073cf49ed704e958b8a66d540dea948>:0) System.Delegate.CreateDelegate (System.Type type,System.Reflection.MethodInfo method,System.Boolean throwOnBindFailure) (at <6073cf49ed704e958b8a66d540dea948>:0) System.Reflection.MethodInfo (System.Type type,method) (在en20 20#)

这是我的密码:

代码语言:javascript
运行
复制
    for (int i = 0; i < statusMoves.Length; i++)
    {
        StatusMovesMethods moveMethods = new StatusMovesMethods();

        MethodInfo theMethod = moveMethods.GetType().GetMethod(statusMoves[i].name);

        moveMethod = (Move.MoveMethod)Delegate.CreateDelegate(typeof(Move.MoveMethod), theMethod);
        statusMoves[i].moveMethod = moveMethod;
    }

这就是初始化moveMethod的地方。

代码语言:javascript
运行
复制
public delegate void MoveMethod(Battler target);
public MoveMethod moveMethod;
EN

Stack Overflow用户

回答已采纳

发布于 2022-05-02 09:19:37

所以,我仍然不知道它为什么会给我一个错误,但我找到了一种方法来修复它,我发现This post中有人有类似的问题,然后我使用了下面的代码:

代码语言:javascript
运行
复制
void Start()
{
    for(int i = 0; i < statusMoves.Length; i++)
    {
        statusMoves[i].moveMethod = GetByName(moveMethods, statusMoves[i].name);
    }
}

static Action GetByName(object target, string methodName)
{
    MethodInfo method = target.GetType()
        .GetMethod(methodName,
                   BindingFlags.Public
                   | BindingFlags.Instance
                   | BindingFlags.FlattenHierarchy);

    // Insert appropriate check for method == null here

    return (Action)Delegate.CreateDelegate
        (typeof(Action), target, method);
}

希望这能帮到别人

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72075156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档