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 question。 GetFields
メソッドは、常に空の列挙を返します。
[EDIT]私は普遍的なWindowsプラットフォームプロジェクト
typeof(CheckBox).GetFields() {System.Reflection.FieldInfo[0]}
typeof(CheckBox).GetProperties() {System.Reflection.PropertyInfo[98]}
typeof(CheckBox).GetMembers() {System.Reflection.MemberInfo[447]}
に次の行を実行するには、バグしているように見えますか?
GetFieldsは私にとっては効果的ですが、当然のことながら、基本クラスではなく、 'type'で宣言されたDependencyPropertyフィールドのみを返します。それに加えて、 'p.FieldType.Equals(typeof(DependencyProperty))'と 'field.FieldType == typeof(DependencyProperty)'の両方を使用することは疑わしいですね。 'typeof(DependencyProperty).IsAssignableFrom(f.FieldType))'を書いたほうがいいです。 – Clemens
私はデバッガにいて、typeof(ListBox).GetFields()を実行すると空のリストが表示されます。 – Briefkasten