13

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.
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Yuriy
  • 2,670
  • 6
  • 33
  • 48

5 Answers5

16

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 ];
            }
        }
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • 2
    ou... nice, and I can write the System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties( this ); in my constructor to use then only once... thanks!! – Yuriy Jan 18 '11 at 10:36
11

Here's a re-usable conversion function for those who got to this post looking for a general function:

public static PropertyDescriptor GetPropertyDescriptor(PropertyInfo PropertyInfo)
{
    return TypeDescriptor.GetProperties(PropertyInfo.DeclaringType).Item(PropertyInfo.Name);
}

and here's an extension method:

public static PropertyDescriptor PropertyDescriptor(this PropertyInfo propertyInfo)
{
  return TypeDescriptor.GetProperties(propertyInfo.DeclaringType)[propertyInfo.Name];
}
toddmo
  • 20,682
  • 14
  • 97
  • 107
5

I found that the following worked:

        //  get property descriptions
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties ( this );

        //  get specific descriptor
        PropertyDescriptor property = properties.Find ( PropertyName, false );

where PropertyName is a value passed into a method.

IAbstract
  • 19,551
  • 15
  • 98
  • 146
0

How about this?

this.GetType().GetProperty("MyProperty")

I think you're asking if you can do this without the string - i.e. some other token that represents 'this property'. I don't think that exists. But since you are writing this code anyway (?) what is the difficulty in just putting the name of the property in the code?

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • 2
    Is this case I'll get PropertyInfo... but I need TypeConverter, DisplayName from my Property.. – Yuriy Jan 18 '11 at 10:22
  • also I can manually get all attributes and find typeConverter, but this is not good idea – Yuriy Jan 18 '11 at 10:24
  • also I can use TypeDescriptor.GetProperties( this ) and then run over all properties and find what I need... but maybe something buit-in was already implemented... and more quickly than that I wrote.. – Yuriy Jan 18 '11 at 10:26
  • Your code isn't refactoring friendly. Use a lamda instead of a string. – CodesInChaos Jan 18 '11 at 10:27
  • 1
    @CodeInChaos - how could you do this using a lambda? Could you post an example please? – James Gaunt Jan 18 '11 at 10:30
0

For future reference, you can now do this:

public static PropertyDescriptor? GetPropertyDescriptor(
    this object target, 
    [CallerMemberName] string propertyName = ""
) => TypeDescriptor.GetProperties(target.GetType())
        .Find(propertyName, ignoreCase: false)
Mike Christiansen
  • 1,104
  • 2
  • 13
  • 30