Get DependencyProperties Using Reflection (Type.GetProperties)?

I have a custom type that is like a pair of fields, and I would like to get only dependency properties.

Here is the code that returns all the properties:

propertyInfos = myType.GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { Console.WriteLine(propertyInfo.Name); } 

I know that I need to add something to the parameter for GetProperties, somethg with BindingFlags.XXX, but I am checking everything that is possible, like XX and have not found something that sounds good to me ...

+4
source share
1 answer

Dependency properties are static fields of type DependencyProperty

 static IEnumerable<FieldInfo> GetDependencyProperties(Type type) { var dependencyProperties = type.GetFields(BindingFlags.Static | BindingFlags.Public) .Where(p => p.FieldType.Equals(typeof(DependencyProperty))); return dependencyProperties; } 

If you want to get the dependency properties of the parents of this control, then you can use the following method:

 static IEnumerable<FieldInfo> GetDependencyProperties(Type type) { var properties = type.GetFields(BindingFlags.Static | BindingFlags.Public) .Where(f=>f.FieldType == typeof(DependencyProperty)); if (type.BaseType != null) properties = properties.Union(GetDependencyProperties(type.BaseType)); return properties; } 
+5
source

All Articles