2012-07-23 6 views
5

인터페이스를 구현하는 객체가 포함 된 모든 속성을 찾고 객체에 대한 메소드를 실행하려고합니다.RuntimePropertyInfo에서 객체를 얻는 방법은 무엇입니까?

Unable to cast object of type 'System.Reflection.RuntimePropertyInfo' to type 'ConfigurationServices.ViewModels.IAmSearchable'.

가 어떻게 오히려 RuntimePropertyInfo보다, 실제 객체를 얻을 수 있습니다 :

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); 
    } 
} 

불행하게도, 나는 오류가 발생합니다 : 이것은 내가 지금까지 가지고있는 코드는? ,

object value = propertyInfo.GetValue(this, null); 

this은 호텔의 "대상"이며, null은 단지 매개 변수가없는 속성을 기대하고 있음을 나타냅니다 :

답변

12

당신은 GetValue 방법으로 속성에서 값을 얻을 필요 인덱서가 아닙니다.

+0

감사합니다. 당신의 설명이 나를 "클릭"하게 만들었습니다. – Migs

관련 문제