DanCaparroz
DanCaparroz

Reputation: 820

Get Attribute info with generics

actually, I can make a relation between a table field and a variable by doing this inside my OE:

public class MyOE
{
  [Column("AGE_FIELD")]
  public int ageField { get; set; }
}

My OE class just need to use this other class:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public class ColumnAtt : Attribute
{
  private string name;

  public string Name
  {
    get { return name; }
  }

  public ColumnAtt (string name)
  {
     this.name = name;
  }  
}

Well, using the code above, Im doing a generic method that I will need to get the "Column" value. How I could do that?

Here is my method:

public void CompareTwoObjectsAndSaveChanges<TObjectType>(TObjectType objectA, TObjectType objectB )
{
    if(objectA.GetType() == objectB.GetType())
    {
       foreach (var prop in objectA.GetType().GetProperties())
       {
           if(prop.GetValue(objectA, null) != prop.GetValue(objectB, null))
           {
               string colvalue  = "";//Here I need to get the Column value of the attribute.
               string nameOfPropertyThatChanges = prop.Name;
               string objectAValue = prop.GetValue(objectA, null).ToString();
               string objectBValue = prop.GetValue(objectB, null).ToString();

           }
       }   
    }
}

Upvotes: 6

Views: 9342

Answers (3)

Samuel Slade
Samuel Slade

Reputation: 8623

You need to use reflection to get the attributes applied to your object. If you know the attribute is always going to be ColumnAtt, then you can do something like this to get the value:

public void CompareTwoObjectsAndSaveChanges<TObjectType>(TObjectType objectA, TObjectType objectB )
{
    if(objectA.GetType() == objectB.GetType())
    {
       foreach (var prop in objectA.GetType().GetProperties())
       {
           if(prop.GetValue(objectA, null) != prop.GetValue(objectB, null))
           {
               // Get the column attribute
               ColumnAttr attr = (ColumnAttr)objectA.GetType().GetCustomAttributes(typeof(ColumnAttr), false).First();

               string colValue = attr.Name;
               string nameOfPropertyThatChanges = prop.Name;
               string objectAValue = prop.GetValue(objectA, null).ToString();
               string objectBValue = prop.GetValue(objectB, null).ToString();
           }
       }   
    }
}

This makes use of the GetCustomAttributes(...) method.

Upvotes: 5

Ivo
Ivo

Reputation: 8372

Try this:

var columnAtt = prop.GetCustomAttributes(typeof(CustomAtt),true).Cast<ColumnAtt>().FirstOrDefault();

(fixed)

Upvotes: 2

ken2k
ken2k

Reputation: 49013

Use reflection:

    private static void Main(string[] args)
    {
        MyOE zz = new MyOE { ageField = 45 };

        foreach (PropertyInfo property in zz.GetType().GetProperties())
        {
            // Gets the first attribute of type ColumnAttribute for the property
            // As you defined AllowMultiple as true, you should loop through all attributes instead.
            var attribute = property.GetCustomAttributes(false).OfType<ColumnAttribute>().FirstOrDefault();
            if (attribute != null)
            {
                Console.WriteLine(attribute.Name);    // Prints AGE_FIELD
            }
        }

        Console.ReadKey();
    }

Upvotes: 6

Related Questions