我有这个escenario:
我想调用一个访问List参数的方法。
参数:列表当我执行以下行时:
MethodInfo methodInfo = type.GetMethod(nameMethod);
object classInstance = Activator.CreateInstance(type, null);
object[] parametersArray = new object[] { Parameter == null ? Activator.CreateInstance(tipoEntidad, null) : Parameter };
objeto = methodInfo.Invoke(classInstance, parametersArray);
它抛出了这个错误:
innerException:{"El formateador inicióuna excepción al intentar反序列化器elmensaje:反序列化器el parámetro的意图错误链接:tempuri.org/lpdv.El mensaje de InnerException era"错误en la"línea 1,位置342。El elemento'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType'连续数据与名称一致'http://schemas.datacontract.org/2004/07/Entitys:Pdv"。El反序列化程序埃斯特的名字。Intente usar DataContractResolver o agregar el tipo"PuntoVenta"的通讯员ejemplo,usando el atributo KnownType属性o agregándolo a lalista de tipos conocidos que se pasa a DataContractSerializer)。咨询公司获得信息的例外情况。"}
只有当参数为List时才会发生这种情况,如果参数的Pdv正确工作,我该怎么办?
这对我有效
[Test]
public void T1()
{
var type = typeof (classwithListmethod);
MethodInfo methodInfo = type.GetMethod("List");
object classInstance = Activator.CreateInstance(type, null);
var list = new List<string>(){"one","two"};
var a = methodInfo.Invoke(classInstance, new []{list});
}
public class classwithListmethod
{
public void List(List<string> s)
{
foreach (var ss in s)
{
Console.WriteLine(ss);
}
}
}