Can i use VS2010 PrivateObject to access a static field inside a static class?

后端 未结 3 729
甜味超标
甜味超标 2021-02-12 13:50

Is it possible to get access to a private static field inside a static class, using the VS2010 Unit Test class PrivateObject ?

Let say i have the following class:

<
3条回答
  •  逝去的感伤
    2021-02-12 14:34

    The property value can be retreived using reflection. This will require the use of Type.GetField Method (String, BindingFlags) and the FieldInfo.GetValue Method

    string propertyName = "bar";
    FieldInfo fieldInfo = typeof(foo).GetField(propertyName, BindingFlags.NonPublic | BindingFlags.Static);
    object fieldValue = fieldInfo.GetValue(null);
    

提交回复
热议问题