我试图根据输入字符串动态创建对象。字符串到类型的映射是在_l
预先构建的。
class A {....}
class B {....}
var _l = new Dictionary<string, Type> { { "1", A } .... } // Error
// 'A' is a type but is used like a 'variable'
PropertyInfo propertyInfo = _l["0"].GetProperty("xxxx");
ObjectType instance = (ObjectType)Activator.CreateInstance(objectType)
propertyInfo.SetValue(instance,
Convert.ChangeType(value, propertyInfo.PropertyType), null);
然而,我得到了错误
&#39; A&#39;是一种类型,但用作变量&#39;
答案 0 :(得分:3)
您需要typeof(A)
var _l = new Dictionary<string, Type> { { "1", typeof(A) } };
答案 1 :(得分:3)
使用:
var _l = new Dictionary<string, Type> { { "1", typeof(A) } .... }
代替