2012-04-29 2 views
2

사용자 정의 속성을 만들고 Core.dll에 저장했습니다.PropertyInfo 다른 DLL의 GetCustomAtributes

public class DBColumnReference : Attribute 
{ 
    string m_column; 


    public string ColumnName { 
     get { return m_column; } 

    } 

    public DBColumnReference(string column) 
    { 
     m_column = column; 
    } 
} 

그럼 Core.dll을 참조하는 앱을 만들었습니다.
내 응용 프로그램에서 자신의 개체를 만들고 일부 속성에서 Core.dll의 사용자 지정 특성을 사용합니다.

public class TestingObject4 
{ 
    string m_table = "TESTING_CORE_OBJECT4"; 

    public string Table 
    { 
     get { return m_table; } 
    } 


    private int m_id = 0; 

    [DBColumnReference("ID")] 
    public int Id 
    { 
     get { return m_id; } 
     set { m_id = value; } 
    } 

는 I 핵심 방법 "FilterProperties (대해서 typeof (TestingObject4))"속성에 의해 필터 특성을 호출한다.

private static Dictionary<string, PropertyInfo> FilterProperties(Type type) 
{ 
    Dictionary<string, PropertyInfo> result = new Dictionary<string, PropertyInfo>(); 
    if(type == null) 
    return result; 

    PropertyInfo[] properties = type.GetProperties(); 
    foreach(PropertyInfo prop in properties) 
    { 
    // Attribute[] atributes = Attribute.GetCustomAttributes(prop, true); 
    object[] atributes = prop.GetCustomAttributes(typeof(DBColumnReference), true); 
    if(atributes != null && atributes.Length != 0) 
    { 
     DBColumnReference reference = atributes[0] as DBColumnReference; 
     result.Add(reference.ColumnName, prop); 
    } 
    } 
    return result; 
} 

그리고 Attributes[] 속성은 항상 비어 있습니다. 속성을 올바르게 가져 오는 방법은 무엇입니까?

+1

당신이에서 속성을 얻고있는 유형을 게시 할 수 있을까요? – Xharze

+0

질문에 추가되었습니다. –

+0

타입 ('object [] atributes = prop.GetCustomAttributes (true);')을 제공하지 않고'GetCustomAttributes()'를 시도해보고 어떻게 될까요? –

답변

1

이 스 니펫을 사용해보세요. 저에게 좋습니다!

public class DBColumnReference : Attribute 
{ 
    string m_column; 


    public string ColumnName 
    { 
     get { return m_column; } 

    } 

    public DBColumnReference(string column) 
    { 
     m_column = column; 
    } 
} 
public class TestingObject4 
{ 
    string m_table = "TESTING_CORE_OBJECT4"; 

    public string Table 
    { 
     get { return m_table; } 
    } 


    private int m_id = 0; 

    [DBColumnReference("an integer id")] 
    public int Id 
    { 
     get { return m_id; } 
     set { m_id = value; } 
    } 
} 
class Program 
{ 
    private static Dictionary<string, PropertyInfo> FilterProperties(Type type) 
    { 
     Dictionary<string, PropertyInfo> result = new Dictionary<string, PropertyInfo>(); 
     if (type == null) 
      return result; 

     PropertyInfo[] properties = type.GetProperties(); 
     foreach (PropertyInfo prop in properties) 
     { 
      // Attribute[] atributes = Attribute.GetCustomAttributes(prop, true); 
      object[] atributes = prop.GetCustomAttributes(typeof(DBColumnReference), true); 
      if (atributes != null && atributes.Length != 0) 
      { 
       DBColumnReference reference = atributes[0] as DBColumnReference; 
       result.Add(reference.ColumnName, prop); 
      } 
     } 
     return result; 
    } 

    static void Main(string[] args) 
    { 
     Dictionary<string, PropertyInfo> resultCollection = FilterProperties(typeof(TestingObject4)); 
     foreach (var singleObject in resultCollection) 
     { 
      Console.WriteLine(singleObject.Key + " " + singleObject.Value); 
     } 
     Console.ReadKey(false); 
    } 
} 
+0

홀 코드를 복사하지 마십시오. 짧은 코드와 설명을 변경하면 충분하다고 생각하여 읽기 쉽도록 만듭니다. –