2016-09-18 1 views
0

컨트롤의 모든 종속성 속성을 가져오고 싶습니다. 또한 유사한 유래 question리플렉션을 사용하여 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"); 
      } 
     } 
    } 
} 

: 내가 좋아하는 뭔가를 시도했다. GetFields 메서드는 항상 빈 열거 형을 반환합니다.

는 는

[편집] 난 보편적 인 윈도우 플랫폼 프로젝트

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 속성으로 정의 된 것처럼 보입니다. 예를 들어 표현

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

빈을 IEnumerable을 반환 그래서 동안

public static DependencyProperty SelectionModeProperty { get; } 

로 선언 된 SelectionModeProperty 재산,

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

하나 개의 요소를 IEnumerable 반환 식을 참조하십시오 즉 위에서 언급 한 SelectionModeProperty입니다.

DependencyProperty 필드/속성의 전체 목록을 얻으려면 모든 기본 클래스를 조사해야합니다.

관련 문제