C# 通过传递字段名来指定字段值

C# 通过传递字段名来指定字段值,c#,.net,reflection,field,C#,.net,Reflection,Field,我一直在读其他的例子,我似乎可以通过输入字段的名称来分配字段的值 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) { fi

我一直在读其他的例子,我似乎可以通过输入字段的名称来分配字段的值

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);         
    }   
}

字段总是以null结尾,我不明白为什么.Net反射中的
Get*
方法在默认情况下只搜索公共成员。
要获取私有字段,请传递
BindingFlags.NonPublic | BindingFlags.Instance