2

当我尝试这样做时,我有一个带有当前对象中字段名称的 ArrayList:

foreach(string name in array)
{
  PropertyInfo property =  this.GetType().GetProperty(name);
  property.SetValue(this, value , null);
}

执行失败,并且该属性没有值(在 SharpDevelop 中只说“null”)。字段名称正常且存在,会发生什么?

4

1 回答 1

0

您的属性是否有设置器(set访问器)。最好在设置属性之前检查一下

if(property != null && property.CanWrite)
{
    property.SetValue(this, value , null);
}

顺便说一句,它是您要访问的字段或属性吗?既然你说字段的名称是好的并且存在

于 2016-08-07T00:33:35.673 回答