C# DynamicInvoke引发参数计数不匹配

C# DynamicInvoke引发参数计数不匹配,c#,C#,我迷路了,觉得自己可能要发疯了。在下面的代码中,TestGetCountry()工作正常,但TestGetState()抛出“参数计数不匹配”异常。我不明白为什么我在一个方法中得到异常,但在另一个方法中却没有。委托使用相同的签名,传递相同的参数类型(字符串[]) StringGenerator委托如下所示: public delegate string StringGenerator(object container); public static string GetCountry(

我迷路了,觉得自己可能要发疯了。在下面的代码中,TestGetCountry()工作正常,但TestGetState()抛出“参数计数不匹配”异常。我不明白为什么我在一个方法中得到异常,但在另一个方法中却没有。委托使用相同的签名,传递相同的参数类型(字符串[])

StringGenerator委托如下所示:

public delegate string StringGenerator(object container);
    public static string GetCountry(object container)
    {
        return Address.GetCountry();
    }
    public static string GetState(object container)
    {
        string[] states = (string[])container;
        int index = SeedGovernor.GetRandomInt(states.Length);
        return states[index];
    }
GetCountry方法如下所示:

public delegate string StringGenerator(object container);
    public static string GetCountry(object container)
    {
        return Address.GetCountry();
    }
    public static string GetState(object container)
    {
        string[] states = (string[])container;
        int index = SeedGovernor.GetRandomInt(states.Length);
        return states[index];
    }
GetState方法如下所示:

public delegate string StringGenerator(object container);
    public static string GetCountry(object container)
    {
        return Address.GetCountry();
    }
    public static string GetState(object container)
    {
        string[] states = (string[])container;
        int index = SeedGovernor.GetRandomInt(states.Length);
        return states[index];
    }

字符串[]
可转换为
对象
,因此此行:

string actual = (string) action.DynamicInvoke(args);
。。。正在使用两个参数调用委托。你想要这个:

string actual = (string) action.DynamicInvoke((object) args);

。。。这样它就可以扩展为创建单个元素
对象[]
,其唯一元素是对字符串数组的引用。

GetCountry
-delegate?我所看到的只是一个
StringGenerators。GetStringGenerator
@HimBromBeere这些是动态调用的方法。为了将来的参考,值得提供一个而不是零碎的方法。