When trying to create a delegate with methodinfo.createdelegate I get and error: 'ArgumentException: method arguments are incompatible

When trying to create a delegate with methodinfo.createdelegate I get and error: 'ArgumentException: method arguments are incompatible

我正在尝试使用 Delegate.CreateDelegate 创建一个代理我收到错误:

A​​rgumentException: 方法参数不兼容 System.Delegate.CreateDelegate(System.Type 类型,System.Object firstArgument,System.Reflection.MethodInfo 方法,System.Boolean throwOnBindFailure,System.Boolean allowClosed)(在 <6073cf49ed704e958b8a66d540dea948>:0 ) System.Delegate.CreateDelegate(System.Type 类型,System.Reflection.MethodInfo 方法,System.Boolean throwOnBindFailure)(位于 <6073cf49ed704e958b8a66d540dea948>:0) System.Delegate.CreateDelegate(System.Type类型,System.Reflection.MethodInfo方法)(在<6073cf49ed704e958b8a66d540dea948>:0)

这是我的代码:

    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 的地方

public delegate void MoveMethod(Battler target);
public MoveMethod moveMethod;

所以我仍然不知道为什么它会给我一个错误,但我找到了修复它的方法,我发现 This post 那里有人有类似的问题,我使用了那里的代码,就像这样:

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);
}

希望这对某人有所帮助