Search code examples
c#.netpropertydescriptor

How to get PropertyDescriptor for current property?


How can I get the PropertyDescriptor for the current property? For example:

[MyAttribute("SomeText")]
public string MyProperty
{
    get{....}
    set
    {
        // here I want to get PropertyDescriptor for this property.
    }
}

Solution

  • You could try this:

    
            public string Test
            {
                get
                {
                    //Get properties for this
                    System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties( this );
    
                    //Get property descriptor for current property
                    System.ComponentModel.PropertyDescriptor pd = pdc[ System.Reflection.MethodBase.GetCurrentMethod().Name ];
                }
            }