How do I change the value of a static readonly field using reflection in c#?

后端 未结 2 1561
借酒劲吻你
借酒劲吻你 2021-01-12 11:47

The SetFields method in the fieldInfo class takes objects as the first parameter. Is there a way to change the value of the static readonly fields using reflection in C#?

2条回答
  •  我在风中等你
    2021-01-12 12:09

    You're close. Your BindingFlag is incorrect. Instance means instance field Instead, you should use BindingFlags.Static:

    var field = typeof(ClassName).GetField("FieldName",BindingFlags.Static|BindingFlags.NonPublic);
    

提交回复
热议问题