2012-08-26 40 views
0

我一直在阅读这个的其他例子,我似乎可以通过传递它的名字来赋值我的字段的值。通过传递它的名字来指定一个字段值

private string fieldName; //contains the name of the field I want to edit 

void IObserver.Update(object data) 
{  
    FieldInfo field = this.GetType().GetField(fieldName); 

    if(field != null) 
    { 
     field.SetValue(this, data);   
    } 
} 

场最终总是空,我无法弄清楚,为什么

+1

请向我们展示这堂课。 – SLaks

回答

2

Get*方法在.net反射只会默认搜索公共成员。
要获得私人领域,请通过BindingFlags.NonPublic | BindingFlags.Instance

相关问题