2013-03-18 3 views
2

예 :이 클래스 내가 propHead 및 propControl을 제외해야클래스 속성 컬렉션을 필터링하는 방법은 무엇입니까?

public class MyClass 
{ 
    private string propHead; 
    private string PropHead { get; set; } 

    private int prop01; 
    private int Prop01 { get; set; } 

    private string prop02; 
    private string Prop02 { get; set; } 

    // ... some more properties here 

    private string propControl; 
    private string PropControl { get; } // always readonly 
} 

있습니다. 는 propControl를 제외하려면 :

이제
MyClass mc = new MyClass(); 
PropertyInfo[] allProps = mc.GetType().GetProperties().Where(x => x.CanWrite).ToArray(); 

, 내가 propHead? 접근성의 모든 주와 동일한 수준을 제외 할 수있는 방법. propHead에 다른 특성을 추가 할 수있는 특수 특성을 추가 할 수있는 방법이 있습니까? 속성 이름은 항상 각 클래스마다 다릅니다.

의견이 있으면 매우 감사하겠습니다.

답변

1

것은 이것은 가장 쉬운 방법은 다음과 같습니다

MyClass mc = new MyClass(); 
PropertyInfo[] allProps = mc.GetType() 
    .GetProperties() 
    .Where(x => x.Name != "propHead" && x.Name != "propControl") 
    .ToArray(); 

그러나 좀 더 범용 솔루션을 찾고 있다면, 당신은이

public class CustomAttribute : Attribute 
{ 
    ... 
} 

MyClass mc = new MyClass(); 
PropertyInfo[] allProps = mc.GetType() 
    .GetProperties() 
    .Where(x => x.GetCustomAttributes(typeof(CustomAttribute)).Length > 0) 
    .ToArray(); 
을 시도 할 수 있습니다
관련 문제