0
我想基于用户输入文档动态生成设置对象。出于某种原因,SetValue抛出Object与目标类型不匹配,尽管如此。对象与SetValue使用类型不匹配的目标类型
即时尝试实现甚至可能吗?
private void MapProp(string prop, string invalue)
{
var currType = _userAssembly.GetType(_className);
var property = currType.GetProperty(prop, BindingFlags.Public | BindingFlags.Instance);
var value = Convert.ChangeType(invalue, property.PropertyType);
property.SetValue(styleType, value, null);
}
}
目前其试图映射到上述目标:
public class TestObject: ITestObj
{
public string PropertyA {get;set;}
public string PropertyB {get;set;}
}
长途区号
MapProp("PropertyA", "testValue");
和类名的getType = .Assembly.TestObject
您展示的不包括有关目标类型任何东西,也不是源类型的代码。我们无法帮助您基于您的收入。 – krillgar
更新了更多的信息,谢谢。 – user4550364