インターフェイスを実装するオブジェクトを含むすべてのプロパティを検索し、オブジェクトに対してメソッドを実行しようとしています。これは私がこれまでに持っているコードです:
foreach (var propertyInfo in this.GetType().GetProperties()
.Where(xx => xx.GetCustomAttributes(typeof(SearchMeAttribute), false).Any()))
{
if (propertyInfo.PropertyType.GetInterfaces().Any(xx => xx == typeof(IAmSearchable)))
{
// the following doesn't work, though I hoped it would
return ((IAmSearchable)propertyInfo).SearchMeLikeYouKnowIAmGuilty(term);
}
}
残念ながら、次のエラーが発生します。
タイプ'System.Reflection.RuntimePropertyInfo'のオブジェクトをタイプ'ConfigurationServices.ViewModels.IAmSearchable'にキャストできません。
ではなく、実際のオブジェクトを取得するにはどうすればよいRuntimePropertyInfo
ですか?