2016-09-18 4 views
0

コントロールのすべての依存関係プロパティを取得します。私は次のようなものを試しました:リフレクションを使用してuwpの依存関係プロパティを取得できません。

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

public static IEnumerable<BindingExpression> GetBindingExpressions(this FrameworkElement element) 
{ 
    IEnumerable<FieldInfo> infos = element.GetType().GetDependencyProperties(); 

    foreach (FieldInfo field in infos) 
    { 
     if (field.FieldType == typeof(DependencyProperty)) 
     { 
      DependencyProperty dp = (DependencyProperty)field.GetValue(null); 
      BindingExpression ex = element.GetBindingExpression(dp); 

      if (ex != null) 
      { 
       yield return ex; 
       System.Diagnostics.Debug.WriteLine("Binding found with path: “ +ex.ParentBinding.Path.Path"); 
      } 
     } 
    } 
} 

また、同様のstackoverflow questionGetFieldsメソッドは、常に空の列挙を返します。

[EDIT]私は普遍的なWindowsプラットフォームプロジェクト

typeof(CheckBox).GetFields() {System.Reflection.FieldInfo[0]} 
typeof(CheckBox).GetProperties() {System.Reflection.PropertyInfo[98]} 
typeof(CheckBox).GetMembers() {System.Reflection.MemberInfo[447]} 

に次の行を実行するには、バグしているように見えますか?

+0

GetFieldsは私にとっては効果的ですが、当然のことながら、基本クラスではなく、 'type'で宣言されたDependencyPropertyフィールドのみを返します。それに加えて、 'p.FieldType.Equals(typeof(DependencyProperty))'と 'field.FieldType == typeof(DependencyProperty)'の両方を使用することは疑わしいですね。 'typeof(DependencyProperty).IsAssignableFrom(f.FieldType))'を書いたほうがいいです。 – Clemens

+0

私はデバッガにいて、typeof(ListBox).GetFields()を実行すると空のリストが表示されます。 – Briefkasten

答えて

0

UWPでは、静的なDependencyPropertyメンバーは、public staticフィールドの代わりにpublic staticプロパティとして定義されているようです。例えばSelectionModePropertyプロパティを参照して、式

typeof(ListBox).GetFields(BindingFlags.Static | BindingFlags.Public) 
    .Where(f => typeof(DependencyProperty).IsAssignableFrom(f.FieldType)) 

が空のIEnumerableを返しながら、発現

typeof(ListBox).GetProperties(BindingFlags.Public | BindingFlags.Static) 
    .Where(p => typeof(DependencyProperty).IsAssignableFrom(p.PropertyType)) 

は、一つの要素とのIEnumerableを返しそう

public static DependencyProperty SelectionModeProperty { get; } 

として宣言されています上記のSelectionModePropertyを使用します。

DependencyPropertyのフィールド/プロパティの完全な一覧を取得するには、すべての基本クラスも調べる必要があります。

関連する問題