当我尝试这样做时,我有一个包含当前对象中字段名称的 ArrayList:
foreach(string name in array)
{
PropertyInfo property = this.GetType().GetProperty(name);
property.SetValue(this, value , null);
}
执行失败,属性没有值(在 SharpDevelop 中只说“null”)。字段名称正常且存在,发生了什么?
最佳答案
您的属性是否有 setter (set
访问器)。在设置属性之前最好检查一下
if(property != null && property.CanWrite)
{
property.SetValue(this, value , null);
}
顺便说一句,您要访问的是字段还是属性?因为你说字段的名称是正确的并且存在
关于c# - (C#) 如何根据name字段存值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38809843/