Ignas Bagdonas
Ignas Bagdonas

Reputation: 45

PropertyInfo GetCustomAtributes from other dll

I created my custom attributes and placed in Core.dll.

public class DBColumnReference : Attribute
{
    string m_column;


    public string ColumnName {
        get { return m_column; }

    }

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

Then I created my app, which has reference to Core.dll.
Created own object in my app, and on some properties use my custom attribute from 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 call Core method "FilterProperties(typeof(TestingObject4))" which filter properties by attributes.

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

And Attributes[] attributes is always empty. How correctly get attributes?

Upvotes: 2

Views: 182

Answers (1)

2shar
2shar

Reputation: 11

try this snippet it works for me!

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

Upvotes: 1

Related Questions