2013-03-07 5 views
1

참고 : 파생 클래스가 아니라 하위 클래스에 대해 묻습니다.클래스 하위 클래스 또는 객체

기본적으로 내가해야 할 일은 개체의 속성을 확인하고 특정 특성 집합이있는 개체를 확인하는 것입니다.

내가 가진 문제는 특성이 많이

이제
public class ExampleAttribute : Attribute 
{ 
    public object Whatever { get; set; } 
} 

public class MiddleEarth 
{ 
    [Example] 
    public Type EntityType { get; set; } 
} 

public class Elf : MiddleEarth 
{ 
    [Example] 
    public SubClass ItsLateAndImTired { get; set; } 

    public IList<Arg> Args { get; set; } 

    //Need to check properties of this object as well 
    public class SubClass 
    { 
     public object SubProperty { get; set; } 

     [Example] 
     public object SubPropertyWithAttribute { get; set; } 
    } 

    public class Arg 
    { 
     [Example] 
     public string Something { get; set; } 
    } 
} 

, 나는 다음과 같이 그것을 할 노력하고있어 서브 클래스에서 것을 ...하지만 코멘트는하지 않습니다에서 언급 한 이유입니다 직장

public List<string> IterateProperties(object _o) 
{ 
    List<string> problems = new List<string>(); 
    foreach (PropertyInfo info in _o.GetType().GetProperties()) 
    { 
     //All 3 of these will return the exact same thing 
     Type thisType = this.GetType(); 
     Type oType = _o.GetType(); 
     Type infoType = info.ReflectedType; 

     //IsSubClassOf only checks for derived classes, 
     //so it's not the method I'm looking for 
     if (info.ReflectedType.IsSubclassOf(this.GetType())) 
     { 
      object sub = info.GetValue(_o, null); 
      if (sub != null) 
      { 
       problems.AddRange(this.IterateProperties(sub)); 
      } 
     } 

     object[] attributes = info.GetCustomAttributes(typeof(ExampleAttribute), true); 
     foreach (object o in attributes) 
     { 
      if (info.GetValue(_o, null) == null) 
      { 
       problems.Add(String.Format("Attribute {0} in class {1} cannot be null", info.Name, info.ReflectedType.ToString())); 
      } 
     } 
    } 

    return problems; 
} 

아이디어가 있으십니까?

+0

믿는다 "엘프" "중간 지구"? 나는 "엘프"가 "휴머노이드"라고 기대할 것입니다. – zzzzBov

+0

@zzzzBov : ItsLateAndHesTired –

+0

@PieterGeerkens, 그러면 나는 그가 낮잠을 자길 기대합니다 ... ZEN FIRE ZE MISSILES! – zzzzBov

답변

0

잘 모르겠어요,하지만 GetProperties를 방법은 도움이되는 몇 가지 플래그를 가지고 있다고 생각 ...

관련 문제