C# C-如何使用反射调用参数数目可变的静态方法

C# C-如何使用反射调用参数数目可变的静态方法,c#,asp.net,exception,reflection,C#,Asp.net,Exception,Reflection,我想通过反射识别以下方法: String.Concat(params string[] args); 这就是我尝试过的: MethodInfo concatMethod = typeof(string).GetMethod("Concat", new Type[] { typeof(string[]) }); 正在正确标识该方法,但当我尝试调用它时: object concatResult = concatMethod.Invoke(null, new object[] { "A", "B"

我想通过反射识别以下方法:

String.Concat(params string[] args);
这就是我尝试过的:

MethodInfo concatMethod = typeof(string).GetMethod("Concat", new Type[] { typeof(string[]) });
正在正确标识该方法,但当我尝试调用它时:

object concatResult = concatMethod.Invoke(null, new object[] { "A", "B" });
我得到以下例外情况:

TargetParameterCountException: Parameter count mismatch.
还要注意,我将null作为实例参数传递给Invoke方法,这是因为该方法是静态的,因此不需要实例。这种方法正确吗

PS:我想模拟以下调用:

String.Concat("A", "B");

输入数组的每个元素都是该方法的一个参数。您拥有的Concat重载采用单个字符串[]参数,因此您需要:

object concatResult = concatMethod.Invoke(null, new object[] { new string[] { "A", "B" } });

输入数组的每个元素都是该方法的一个参数。您拥有的Concat重载采用单个字符串[]参数,因此您需要:

object concatResult = concatMethod.Invoke(null, new object[] { new string[] { "A", "B" } });

尝试改用新对象[]{new string[]{A,B}}。谢谢,它成功了!:DTry使用新对象[]{new string[]{A,B}}代替。谢谢,它成功了!:谢谢你,工作如期进行。我会尽快把答案记为正确的谢谢你,工作如期进行。我会尽快把答案记为正确答案