如何使用反射的时候我只有属性的字符串名我设置一个类的静态属性?例如我有:
List<KeyValuePair<string, object>> _lObjects = GetObjectsList();
foreach(KeyValuePair<string, object> _pair in _lObjects)
{
//class have this static property name stored in _pair.Key
Class1.[_pair.Key] = (cast using typeof (_pair.Value))_pair.Value;
}
我不知道如何设置属性的值使用属性名称字符串。一切都是动态的。我可以使用列表中的5个项目来设置5个静态属性,每个项目具有不同的类型。
感谢您的帮助。
答:
Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName");
PropertyInfo _propertyInfo = _type.GetProperty("Field1");
_propertyInfo.SetValue(_type, _newValue, null);