gpt4 book ai didi

c# - 比较对象类型的类属性

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:17 24 4
gpt4 key购买 nike

我试图记录一个对象的每个属性的历史记录,只有当它发生变化时才被更新。我没有为每个属性做一个 if 语句,而是决定遍历每个我定义了自定义属性的属性以过滤掉我不想记录的属性,然后比较旧值和新值以查看是否他们已经改变了。

然而,检查每个 obj 案例是否相等似乎很大,而且非常具体,因为我也有枚举类型可以比较。是否有更好的方法来完成此任务或重构我的代码?

代码示例

我的类(class)

public class MyClassDto
{
[Comparible]
public MyCustomType CustomType{ get; set; }

[Comparible]
public DateTime? SomeDate { get; set; }

[Comparible]
public decimal? SomeDecimalValue { get; set; }

[Comparible]
public int? SomeIntValue { get; set; }

// Other Non Custom Attribute Type Properties
}

添加历史辅助函数

public void AddHistory(MyClassDto prevMyObject, MyClassDto newMyObject)
{
if (prevMyObject== null || newMyObject == null) return;

foreach (var property in prevMyObject.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof (ComparibleAttribute))))
{
var fieldName = property.Name;
object prevValue = property.GetValue(prevMyObject, null);
object newValue = newMyObject.GetType().GetProperty(property.Name).GetValue(newMyObject, null);

if (!IsEqual(prevValue, newValue))
{
//Log the field, prevValue, newValue
}
}
}

对象比较函数

   private static bool IsEqual(object obj1, object obj2)
{
if (obj1 == null && obj2 == null || obj1 == obj2) return true;
if (obj1 == null || obj2 == null) return false;

var objType = obj1.GetType();
if (objType == typeof (string))
{
return string.Equals(obj1.ToString(), obj2.ToString());
}
if (objType == typeof (int?))
{
return (Convert.ToInt32(obj1) == Convert.ToInt32(obj2));
}
if (objType == typeof (decimal?) || objType == typeof (decimal))
{
return (decimal.Round(Convert.ToDecimal(obj1), 2) == decimal.Round(Convert.ToDecimal(obj2), 2));
}
if (objType == typeof (DateTime?) || objType == typeof (DateTime))
{
return (Convert.ToDateTime(obj1) == Convert.ToDateTime(obj2));
}
if (objType == typeof (ContractTrackerType))
{
return (((MyCustomType) obj1).Name() == ((MyCustomType ) obj2).Name());
}

return false;
}

最佳答案

您实际上不需要原始类型的单独案例。对于您的自定义类型,您应该让它们覆盖 object.Equals , 以便将相等性测试逻辑封装在类中。

这样,您的IsEqual 函数应该只是对object.Equals 的调用:

 private static bool IsEqual(object obj1, object obj2)
{
return object.Equals(obj1, obj2);
}

来自 object.Equals 的注释:

The static Equals(Object, Object) method indicates whether two objects, objA and objB, are equal. It also enables you to test objects whose value is null for equality. It compares objA and objB for equality as follows:

It determines whether the two objects represent the same object reference. If they do, the method returns true. This test is equivalent to calling the ReferenceEquals method. In addition, if both objA and objB are null, the method returns true.

It determines whether either objA or objB is null. If so, it returns false.

If the two objects do not represent the same object reference and neither is null, it calls objA.Equals(objB) and returns the result. This means that if objA overrides the Object.Equals(Object) method, this override is called.

关于c# - 比较对象类型的类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24945750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com