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