Search code examples
.nettypedescriptorpropertydescriptorgetproperties

TypeDescriptor.GetProperties return nothing from a class


I have defined a class TestObject that contains two simple properties num and name. I am trying to use TypeDescriptor.GetProperties() for the object of TestObject class to retrieve the defined properties. But, it doesn't return anything.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        object selobj = new TestObject();

        foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(selobj))
        {
            string cat = pd.Category;
        }
    }
}


public class TestObject
{
    string name = "Hello World";

    int Num
    {
        get { return 100; }
    }

    string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Am I missing something simple here? Appreciate your help.


Solution

  • Make sure the properties are marked as public