2012-02-02 69 views
2

实际上,我可以这样做我的OE里面做一个表中的字段和变量之间的关系:获取属性信息与仿制药

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

我的OE类只需要使用其他类:

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

那么,使用上面的代码,我做了一个泛型方法,我将需要获得“列”值。我怎么能做到这一点?

这里是我的方法:

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

      } 
     } 
    } 
} 

回答

2

你需要使用反射来获取应用到您的对象的属性。如果您知道属性总是会ColumnAtt,那么你可以做这样的事情来获取值:

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

这使得利用GetCustomAttributes(...)方法。

+0

谢谢,兄弟!出于任何原因,条件if(prop.GetValue(objectA,null)!= prop.GetValue(objectB,null)),我不知道为什么..我比较相同的值,并说它们是不同的(两个大写和相同的打字。) – 2012-02-02 20:19:26

+0

@ Dan-SP它试图比较哪些值?有可能使用'!='运算符,它不能正确比较。 – 2012-02-03 08:17:15

+0

我试图比较两个简单的字符串..它们具有完全相同的值。 – 2012-02-06 17:58:24

2

试试这个:

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

(固定)

+0

'GetCustomAttribute(...)'不存在,它是'GetCustomAttributes(...)';它返回一个数组,而不是一个单一的对象。 – 2012-02-02 13:27:20

+0

固定,谢谢! – ivowiblo 2012-02-02 13:38:54

4

使用反射:

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(); 
    } 
+0

这正是我要发布:) – 2012-02-02 13:31:28