DynamicInvoke throws Parameter count mismatch

I'm lost and feel I may be going crazy. In the below code, TestGetCountry() works just fine but TestGetState() throws the "Parameter count mismatch" exception. I'm lost as to why I'm getting the exception in one method, but not the other. The delegates use the same signature, passed the same argument type (string[]).

    [TestMethod]
    public void TestGetCountry()
    {
        string expected = Address.GetCountry();

        // get the method associated with this Enums.StringGenerator from the dictionary
        Delegate action = StringGenerators.GetStringGenerator(Enums.StringGenerators.COUNTRY);
        string[] args = new string[] { "" };
        string actual = (string)action.DynamicInvoke(args);

        Assert.AreEqual(expected, actual, "Country does not match");
    }

    [TestMethod]
    public void TestGetState()
    {
        string expected = "CA";

        Delegate action = StringGenerators.GetStringGenerator(Enums.StringGenerators.STATE);
        string[] args = new string[] {"CA", "NY"};
        string actual = (string)action.DynamicInvoke(args); // exception thrown here

        Assert.AreEqual(expected, actual, "State does not match");
    }

The StringGenerator delegate looks like this:

public delegate string StringGenerator(object container);

The GetCountry method looks like this:

    public static string GetCountry(object container)
    {
        return Address.GetCountry();
    }

The GetState method looks like this:

    public static string GetState(object container)
    {
        string[] states = (string[])container;
        int index = SeedGovernor.GetRandomInt(states.Length);
        return states[index];
    }
Jon Skeet
people
quotationmark

string[] is convertible to object, so this line:

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

... is invoking the delegate with two arguments. You want this:

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

... so that it expands to create a single-element object[] whose sole element is a reference to the string array.

people

See more on this question at Stackoverflow