4

いくつかのフィールドとしてカスタム タイプを取得しましたが、依存関係プロパティのみを取得したいと考えています。

すべてのプロパティを返すコードは次のとおりです。

propertyInfos = myType.GetProperties();

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

GetProperties のパラメーターに何かを追加する必要があることはわかっていますが、BindingFlags.XXX を使用して何かを追加する必要がありますが、XX として可能なすべてのものをチェックしましたが、私にとって良いと思われるものは見つかりませんでした...

4

1 に答える 1

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;
}
于 2011-09-28T11:58:53.510 に答える