views:

810

answers:

3

I'm trying to use reflection to get a property from a class. Here is some sample code of what I'm seeing:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Reflection.PropertyInfo[] tmp2 
                 = typeof(TestClass).GetProperties();
            System.Reflection.PropertyInfo test 
                 = typeof(TestClass).GetProperty("TestProp", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
        }
    }

    public class TestClass
    {
        public Int32 TestProp
        {
            get;
            set;
        }
    }
}

When I trace through this, this is what I see:

  • When I fetch all properties using GetProperties, the resulting array has one entry, for property TestProp.
  • When I try to fetch TestProp using GetProperty, I get null back.

I'm a little stumped; I haven't been able to find anything in the MSDN regarding GetProperty to explain this result to me. Any help?

EDIT:

If I add BindingFlags.Instance to the GetProperties call, no properties are found, period. This is more consistent, and leads me to believe that TestProp is not considered an instance property for some reason.

Why would that be? What do I need to do to the class for this property to be considered an instance property?

+1  A: 

Add BindingFlags.Instance to the GetProperty call.

EDIT: In response to comment...

The following code returns the property.

Note: It's a good idea to actually make your property do something before you try to retrieve it (VS2005) :)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Reflection.PropertyInfo[] tmp2
                 = typeof(TestClass).GetProperties();
            System.Reflection.PropertyInfo test
                 = typeof(TestClass).GetProperty("TestProp", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);

            Console.WriteLine(test.Name);
        }
    }

    public class TestClass
    {
        public Int32 TestProp
        {
            get
            {
                return 0;
            }
            set
            {
            }
        }
    }
}
Andrew Rollings
If I do that, GetProperties returns nothing as well, which is consistent, in least. But the question is: what do I need to do to find the property with GetProperty? Why isn't TestProp considered an Instance property?
Remi Despres-Smyth
...or what do I need to do to class TestClass so that its property appears as an instance property?
Remi Despres-Smyth
I couldn't duplicate your problem (although I'm using VS2005, so I had to flesh out your property with an implementation).
Andrew Rollings
I can't either. I copied your sample, removed the property implementation, and all worked fine. Pasted my original example back in, and it was working fine too. Tried a number of things to bring the figure out what I did, with no luck. Oh well, thanks for taking the time.
Remi Despres-Smyth
Well vote up/accept the answer then ;)
Andrew Rollings
Patience, grasshopper. I was running more tests on my actual system to see if I was going to see something odd again before accepting.
Remi Despres-Smyth
A: 

You need to specify whether it is static or an instance (or both) too.

leppie
See comment to Andrew Rolling's post, above.
Remi Despres-Smyth
+1  A: 

Try to add the following tag:

System.Reflection.BindingFlags.Instance

EDIT: This works (at least to me)

PropertyInfo test = typeof(TestClass).GetProperty("TestProp", BindingFlags.Public | BindingFlags.Instance);

Console.WriteLine(test.Name);
bruno conde
See comments to Andrew Rolling's post, above.
Remi Despres-Smyth
You must be doing something wrong Remi because, with the example you gave to us this works fine ...
bruno conde
It does for me, now, too. I can't explain it; blame the user :-) Thanks.
Remi Despres-Smyth