cono_mm

I want to get a list of all the Dependency Properties I've declared or overrided within one of my classes.

In my class I've declared 
public class MyClass: Canvas\
{
public static DependencyProperty WidthProperty;
public static DependencyProperty HeightProperty;
public static DependencyProperty MyDProperty;
public static MyClass()
{

static BPMNShape()

{

MyClass.WidthProperty = DependencyProperty.RegisterAttached("WidthProperty", typeof(double), typeof(MyClass), new PropertyMetadata(new PropertyChangedCallback(OnWidthChanged)));MyClass.HeightProperty = DependencyProperty.RegisterAttached("HeightProperty", typeof(double), typeof(MyClass), new PropertyMetadata(new PropertyChangedCallback(OnHeightChanged)));

MyClass.BPMNNameProperty = DependencyProperty.RegisterAttached("MyDProperty", typeof(string), typeof(MyClass), new PropertyMetadata(new PropertyChangedCallback(OnMyDPropertyChanged)));

}

}
}

I've tried

MyClass obj=new MyClass();
  foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj,
    new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }))
  {
    DependencyPropertyDescriptor dpd =
      DependencyPropertyDescriptor.FromProperty(pd);

    if (dpd != null )
    {
      Debug.WriteLine(dpd.Name);
    }
  }
 
 
but it is not working .
 
Can anyone help me with this 



Re: Windows Presentation Foundation (WPF) List Dependency Properties

Yi-Lun Luo - MSFT

Hello, by saying ˇ°it is not workingˇ±, I think you mean you get all DependencyProperties of your class, even those inherited ones. This is the result I got with your code. Since all DependencyProperties are declared as public fields, I think you can try this. ItˇŻll only return those fields declared in your own class:

MyClass obj = new MyClass();

foreach (FieldInfo fi in obj.GetType().GetFields())

{

if(fi.FieldType == typeof(DependencyProperty))

{

Debug.WriteLine(fi.Name);

}

}