//try to set this property .
public List<USER_ROLE> list {get;set;}
var result = new List<USER_ROLE>() { new USER_ROLE()
{ userid = 0,role_id = 1,role_cde = "00001"
}};
//Get property with name of property.
object obj = Activator.CreateInstance(property.PropertyType);
var getva = property.GetValue(obj,null);// Object does not match target type
property.PropertyType.GetMethod("Add").Invoke(obj,new object[] { result });
if (obj.GetType() == property.PropertyType)
{
property.SetValue(obj, property, null);// object does not match target type.
}
运行时execption:对象与目标类型不匹配。我不明白目标类型,请告诉我哪里错了?
答案 0 :(得分:0)
在查看the documentation后,似乎您认为第一个参数是原始对象,第二个参数是您要将属性设置为的值。
看起来你想将属性设置为obj
,在这种情况下,你要做
property.SetValue(/*instance of whatever class has the property*/, obj, null);