38

Is there any way to get value of private static field from known class using reflection?

0

5 Answers 5

83

Yes.

Type type = typeof(TheClass);
FieldInfo info = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Static);
object value = info.GetValue(null);

This is for a field. For a property, change type.GetField to type.GetProperty. You can also access private methods in a similar fashion.

1
  • 1
    It might be worth noting that the static field can also be assigned via info.SetValue(null, value). I used this answer to set a value to a static field.
    – IAbstract
    Jul 16, 2012 at 14:54
4

I suppose someone should ask whether this is a good idea or not? It creates a dependency on the private implementation of this static class. Private implementation is subject to change without any notice given to people using Reflection to access the private implementation.

If the two classes are meant to work together, consider making the field internal and adding the assembly of the cooperating class in an [assembly:InternalsVisibleTo] attribute.

3
  • 3
    In production code this generally is a bad idea. It can however be very useful when unit testing, because you can write tests without having to expose fields you'd rather keep private.
    – AVee
    May 24, 2012 at 11:52
  • 4
    @AVee: for unit testing, make the field internal and use InternalsVisibleTo. Better, unit testing should not test implementation, only correct behavior. By depending on the private field, now the unit test will break if the class implementation changes. May 24, 2012 at 17:35
  • 1
    I agree with @JohnSaunders reflection like this is very brittle. There are cases though where it should exist as a temporary work around - i.e. the production code is already released, yet I needed a special utility to run some acceptance tests. I could only achieve this by accessing a private static field via reflection. This code will change and the reflected class will be modified to allow the interaction required by the new utility in the next release.
    – IAbstract
    Jul 16, 2012 at 14:57
2

As stated above, you can probably use System.Type::GetMembers() with BindingFlags::NonPublic | BindingFlags::Static, but only if you have the right ReflectionPermission.

0

If you have full trust, you should be able to do:

Type t = typeof(TheClass);
FieldInfo field = t.GetField("myFieldName", BindingFlags.NonPublic | BindingFlags.Static);
object fieldValue = field.GetValue(myObject);

However, if you run this on a system without full trust, the GetField call will fail, and this won't work.

0
-1

Try something like this:

Type type = typeof(MyClass);
MemberInfo[] members = type.GetMembers(BindingFlags.NonPublic | BindingFlags.Static);

I would think that is should work.

1
  • MemberInfo does not have GetValue(...) or SetValue(...) methods. Members are more often actual methods/functions.
    – IAbstract
    Jul 16, 2012 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.