ジェネリック型の内部でプロパティを見つける必要があります。これは古い方法です(私のコードはWinRT専用なので、別のアプローチが必要だと思います):
PropertyInfo pi = typeof(TRp).GenericTypeArguments[0].GetProperty(idField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
を使用して同じ結果を達成する必要がありGetRuntimeProperties
ます。これが私のアプローチです:
PropertyInfo pi = typeof(TRp).GenericTypeArguments[0].GetRuntimeProperties().Single(p => p.Name.ToUpper() == idField.ToUpper()...
ご覧のとおりIgnoreCase
、私はカスタムの方法で実装しましたが、おそらくもっとうまくできるでしょうか?
残りを実装するにはどうすればよいBindingFlags
ですか?
ありがとうございました!
あなたは実際にそうする必要はありません。Type.GetRuntimeProperties
実装方法は次のとおりです。
public static IEnumerable<PropertyInfo> GetRuntimeProperties(this Type type)
{
CheckAndThrow(type);
IEnumerable<PropertyInfo> properties = type.GetProperties(everything);
return properties;
}
ここeverything
で、次のように定義されています。
private const BindingFlags everything = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static;
つまり、必要なフラグがすでに検索されているということです。
編集:
BindingFlags
自分で指定したい場合は、独自のカスタム拡張メソッドを作成できます。
public static class TypeExtensions
{
public static IEnumerable<PropertyInfo> GetAllProperties(this TypeInfo type,
BindingFlags bindingFlags)
{
var propertyInfos = type.GetProperties(bindingFlags);
var subtype = type.BaseType;
if (subtype != null)
list.AddRange(subtype.GetTypeInfo().GetAllProperties(bindingFlags));
return propertyInfos.ToArray();
}
}
これはテストされていないことに注意してください。それはあなたがそれを自分でできることをあなたに示す試みにすぎません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加