There is an instance of class Class1 , how can I get it aValue field? The question is extremely stupid, I understand ... and yet. It turns out to get only static fields, and for instances how to be?

 public class Class1{ private int aValue; public Class1(int a){ this.aValue = a; } } Class1 cls = new Class1(10); 
  • I vote for the closure of this issue, since it is set in English, not Russian - cheops
  • one
    I did not even notice that this is a py domain. translated, just so necessary) - Noone Knows
  • habrahabr.ru/post/304520 - vitidev 6:54 pm

2 answers 2

You must specify a mask:

 Class1 cls = new Class1(10); FieldInfo fieldInfo = typeof(Class1).GetField("aValue", BindingFlags.Instance | BindingFlags.NonPublic); int a = (int)fieldInfo.GetValue(cls); 
  • one
    A bit on the contrary did it turned out ... getValue Null passed and not an instance. exactly what is needed. - Noone Knows

One of the principles of OOP is Encapsulation, which means that access to "some" class properties is possible only "inside the capsule" from the class method; Write a method public int GetValue() {return aValue}

  • I do not have full access to the class, for I am writing some modification ... otherwise there would be no need for reflection ... - Noone Knows