[英]Call a Method which has params using Reflection
我正在尝试调用具有使用反射的 Params 的方法。 它返回 System.Reflection.TargetParameterCountException
这仅发生在方法参数中具有 params 关键字的方法上
Public static dynamic Function(JObject data, string PathFunction) {
string MethodName = "MergeFields";
string FunctionsNamespace ="Test.Functions";
Object[] parameterArray = {"274-84-3068","5","1","Hugenberg","4","0"}
// Call Static class functions
Type type = Type.GetType(FunctionsNamespace);
Object obj = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod(MethodName);
object st = methodInfo.Invoke(obj, parameterArray);
return st;
}
public static string MergeFields(params string[] data)
{
StringBuilder sb = new StringBuilder();
// code to be processed
return sb.ToString();
}
如果你有一个方法:
public static string MergeFields(params string[] data)
你打电话给:
MergeFields("a", "b", "c");
编译器偷偷把它变成:
MergeFields(new string[] { "a", "b", "c" });
但是,当您使用反射时,您不会在此处获得编译器的帮助:您需要自己创建该字符串数组:
object[] parameterArray = new object[] { new string[] { "274-84-3068", "5", "1", "Hugenberg", "4", "0" } };
在这里,我们将向MergeFields
传递一个参数,该参数是一个字符串数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.