mardi 8 novembre 2022

How to Invoke a static method and provide only some of optional parameters?

This is my static class containing a static method:

public static class TS
{
    public static bool act(string s, string t = "def", int id = 0)
    {
        return true;
    }
}

And this is how I invoke it somewhere in the code:

string name = "act";
typeof(TS).GetMethod(name).Invoke(null, new object[] { "foo", "bar" });

Note that the first optional parameter is given but the second one is not. if I provide them all it works just fine but now I get this exception:

Exception thrown: 'System.Reflection.TargetParameterCountException' in mscorlib.dll

Additional information: Parameter count mismatch.

How do I tell it to use the default values for some of optional parameters?





Aucun commentaire:

Enregistrer un commentaire