2012-04-29 9 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; } 
    } 

私はコアメソッド "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

穴のコードをコピーしないでください。私は短いコードとあなたが変更したものは十分だと思います。 –

関連する問題