2011-09-28 10 views
4

いくつかのフィールドとしてカスタムタイプを取得しましたが、依存プロパティのみを取得したいと考えています。リフレクション(Type.GetProperties)を使用してDependencyPropertiesを取得しますか?

propertyInfos = myType.GetProperties(); 

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

は私がBindingFlags.XXXとGetPropertiesのためのパラメータ、somethgに何かを追加する必要があります知っているが、私はXXとして可能であるすべてのものをチェックしませんでした。ここで

は、すべてのプロパティを返すコードですあなたが取得したい場合は私にはいいですね何かを見つける...

+0

は、依存関係プロパティの二つの側面があります:そのコントロールの親の依存関係プロパティは、あまりにも、あなたは以下の方法を使用することができます(それは 'タイプのものであり、実際の依存関係プロパティである静*フィールド*をDependencyProperty' )とその静的フィールドの値を返すfacadeプロパティ。あなたは何を返そうとしていますか? –

答えて

5

依存関係プロパティは、タイプDependencyProperty

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

の静的フィールドです

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; 
} 
関連する問題