Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Attribute info with generics

Tags:

c#

c#-4.0

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

           }
       }   
    }
}
like image 404
Dan-SP Avatar asked Feb 02 '12 13:02

Dan-SP


2 Answers

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();
    }
like image 177
ken2k Avatar answered Sep 20 '22 08:09

ken2k


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.

like image 25
Samuel Slade Avatar answered Sep 23 '22 08:09

Samuel Slade