C# 使用泛型获取属性信息
实际上,我可以在OE中建立表字段和变量之间的关系:C# 使用泛型获取属性信息,c#,c#-4.0,C#,C# 4.0,实际上,我可以在OE中建立表字段和变量之间的关系: public class MyOE { [Column("AGE_FIELD")] public int ageField { get; set; } } 我的OE类只需要使用另一个类: [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)] public class ColumnAtt : Attribute { pr
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();
}
}
}
}
public void compareTwoObject和saveChanges(TObjectType对象A、TObjectType对象B)
{
if(objectA.GetType()==objectB.GetType())
{
foreach(objectA.GetType().GetProperties()中的var prop)
{
if(prop.GetValue(objectA,null)!=prop.GetValue(objectB,null))
{
string colvalue=”“;//这里我需要获取属性的列值。
PropertyThatChanges的字符串名称=prop.Name;
字符串objectAValue=prop.GetValue(objectA,null).ToString();
字符串objectBValue=prop.GetValue(objectB,null).ToString();
}
}
}
}
试试这个:
var columnAtt = prop.GetCustomAttributes(typeof(CustomAtt),true).Cast<ColumnAtt>().FirstOrDefault();
var columnAtt=prop.GetCustomAttributes(typeof(CustomAtt),true.Cast().FirstOrDefault();
(已修复)您需要使用反射来获得应用于对象的属性。如果您知道该属性始终是
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();
}
}
}
}
public void compareTwoObject和saveChanges(TObjectType对象A、TObjectType对象B)
{
if(objectA.GetType()==objectB.GetType())
{
foreach(objectA.GetType().GetProperties()中的var prop)
{
if(prop.GetValue(objectA,null)!=prop.GetValue(objectB,null))
{
//获取列属性
ColumnAttr attr=(ColumnAttr)objectA.GetType().GetCustomAttributes(typeof(ColumnAttr),false).First();
字符串colValue=attr.Name;
PropertyThatChanges的字符串名称=prop.Name;
字符串objectAValue=prop.GetValue(objectA,null).ToString();
字符串objectBValue=prop.GetValue(objectB,null).ToString();
}
}
}
}
这将使用该方法。使用反射:
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();
}
private static void Main(字符串[]args)
{
MyOE zz=新的MyOE{ageField=45};
foreach(zz.GetType().GetProperties()中的PropertyInfo属性)
{
//获取属性的ColumnAttribute类型的第一个属性
//正如您将AllowMultiple定义为true一样,您应该遍历所有属性。
var attribute=property.GetCustomAttributes(false).OfType().FirstOrDefault();
if(属性!=null)
{
Console.WriteLine(attribute.Name);//打印年龄字段
}
}
Console.ReadKey();
}
这正是我要发布的内容:)谢谢,兄弟!!不管出于什么原因,如果条件是(prop.GetValue(objectA,null)!=prop.GetValue(objectB,null)),我不知道为什么。。我比较相同的值,它说它们是不同的(大写和相同的类型)。@Dan SP它试图比较的值是什么?有可能使用=代码>运算符,比较不正确。我正在尝试比较两个简单字符串。。它们的值完全相同。在比较字符串相等性时,我更喜欢使用。如果不关心大小写,可以使用StringComparison.OrdinalIgnoreCase
作为参数之一。也许值得一试,看看是否有什么不同。谢谢,伙计!因为我不总是收到字符串,所以建议的解决方案是:prop.GetValue(objectA,null)!=prop.GetValue(objectB,null)
运行良好。。您建议如何将其更改为也使用字符串?谢谢