locked
Getting null When using Reflection Method GetProperty

    Question

  • I have the following test code to try and figure out what's going on in another application I am working on:

    public class ReflectionTester
        {
            public static string StaticString = "";
            public int MyInt = 0;
            private int PrivateInt = 10;
    
            public void Reflect()
            {
                Type t = this.GetType();
    
                PropertyInfo myIntProp = t.GetProperty("MyInt");
                PropertyInfo privateIntProp = t.GetProperty("PrivateInt",BindingFlags.NonPublic);
                PropertyInfo staticStringProp = t.GetProperty("StaticString", BindingFlags.Static);
    
            }
        }

    I am new to reflection and don't understand why each one of these calls to GetProperty returns null?  Any help would be appreciated.

    Friday, January 23, 2015 12:29 PM

Answers

  • Because the ReflectionTester doesn't have any properties, it only has fields. Change the fields into properties and use the appropriate flags:

    public class ReflectionTester
      {
        public static string StaticString {
          get;
          set;
        }
        public int MyInt {
          get;
          set;
        }
        private int PrivateInt {
          get;
          set;
        }
    
        public void Reflect() {
          Type t = this.GetType();
    
          PropertyInfo myIntProp = t.GetProperty("MyInt");
          PropertyInfo privateIntProp = t.GetProperty("PrivateInt", BindingFlags.NonPublic | BindingFlags.Instance);
          PropertyInfo staticStringProp = t.GetProperty("StaticString", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
    
        }
      }
    

    Or use the GetField method:

      public class ReflectionTester
      {
        public static string StaticString = "";
        public int MyInt = 0;
        private int PrivateInt = 10;
    
        public void Reflect() {
          Type t = this.GetType();
    
          FieldInfo myIntProp = t.GetField("MyInt");
          FieldInfo privateIntProp = t.GetField("PrivateInt", BindingFlags.NonPublic | BindingFlags.Instance);
          FieldInfo staticStringProp = t.GetField("StaticString", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
    
        }
      }
    

    Please remember to mark helpful posts as answer to close your threads.
    • Marked as answer by S O'Connor Friday, January 23, 2015 2:02 PM
    Friday, January 23, 2015 1:58 PM

All replies

  • Because the ReflectionTester doesn't have any properties, it only has fields. Change the fields into properties and use the appropriate flags:

    public class ReflectionTester
      {
        public static string StaticString {
          get;
          set;
        }
        public int MyInt {
          get;
          set;
        }
        private int PrivateInt {
          get;
          set;
        }
    
        public void Reflect() {
          Type t = this.GetType();
    
          PropertyInfo myIntProp = t.GetProperty("MyInt");
          PropertyInfo privateIntProp = t.GetProperty("PrivateInt", BindingFlags.NonPublic | BindingFlags.Instance);
          PropertyInfo staticStringProp = t.GetProperty("StaticString", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
    
        }
      }
    

    Or use the GetField method:

      public class ReflectionTester
      {
        public static string StaticString = "";
        public int MyInt = 0;
        private int PrivateInt = 10;
    
        public void Reflect() {
          Type t = this.GetType();
    
          FieldInfo myIntProp = t.GetField("MyInt");
          FieldInfo privateIntProp = t.GetField("PrivateInt", BindingFlags.NonPublic | BindingFlags.Instance);
          FieldInfo staticStringProp = t.GetField("StaticString", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
    
        }
      }
    

    Please remember to mark helpful posts as answer to close your threads.
    • Marked as answer by S O'Connor Friday, January 23, 2015 2:02 PM
    Friday, January 23, 2015 1:58 PM
  • Ah!  Now it makes sense.  I switched them to properties and it works.  Thank you for that clarification.
    Friday, January 23, 2015 2:02 PM