使用泛型获取属性信息。

6

实际上,在我的 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;
  }  
}

使用上述代码,我正在做一个通用的方法,需要获取“Column”值。我该怎么做?

这是我的方法:

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

           }
       }   
    }
}
3个回答

6

使用反射:


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

那正是我要发布的内容 :) - Pierluc SS

5

您需要使用反射来获取应用于您的对象的属性。如果您知道该属性始终为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(...)方法。

谢谢,兄弟!!由于某种原因,条件if(prop.GetValue(objectA, null) != prop.GetValue(objectB, null)),我不知道为什么..我正在比较相同的值,但它说它们是不同的(都是大写和相同的类型)。 - Dan-SP
@Dan-SP 它正在尝试比较哪些值?使用“!=”运算符可能无法正确比较。 - Samuel Slade
我正在尝试比较两个简单的字符串...它们的值完全相同。 - Dan-SP
在比较字符串相等性时,我更喜欢使用String.Equals(...)。如果您不关心大小写,可以将StringComparison.OrdinalIgnoreCase用作其中一个参数。值得一试,看看是否有所不同。 - Samuel Slade
谢谢,伙计!由于我并不总是接收字符串,你提出的建议解决方案:prop.GetValue(objectA, null) != prop.GetValue(objectB, null) 很好用。你会如何建议修改以使其也适用于字符串?谢谢! - Dan-SP
@Dan-SP 你可以始终进行类型检查以查看你正在处理什么,然后有一系列的方法重载来比较不同类型的值。在这里查看一个我所说的示例。但是,你可能需要在一个单独的项目中对字符串比较进行测试,并查看!===运算符在字符串格式和存储为对象时是否起作用。 - Samuel Slade

2

试试这个:

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

(固定的)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接