Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GetProperty fail to find it?

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


using System.Reflection;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            PropertyInfo[] tmp2 = typeof(TestClass).GetProperties();
            PropertyInfo test = typeof(TestClass).GetProperty(
               "TestProp", BindingFlags.Public | 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?

like image 906
Remi Despres-Smyth Avatar asked Dec 11 '08 18:12

Remi Despres-Smyth


People also ask

What does getProperty method do?

The getProperty(String key) method in Java is used to returns the system property denoted by the specified key passed as its argument.It is a method of the java. lang. System Class.

What is the return type of getProperty?

getProperty(String key) method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

What is getProperty C#?

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints. GetProperty(String) Searches for the public property with the specified name.


2 Answers

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) :)

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

            Console.WriteLine(test.Name);
        }
    }

    public class TestClass
    {
        public Int32 TestProp
        {
            get
            {
                return 0;
            }
            set
            {
            }
        }
    }
}
like image 177
Andrew Rollings Avatar answered Sep 28 '22 16:09

Andrew Rollings


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);
like image 45
bruno conde Avatar answered Sep 28 '22 15:09

bruno conde