在 C# 中比较数据类型
Compare Datatype in C#
我想比较两个 classes 和 return bool 值的数据类型。问题是我的方法不比较 class
的 class 内的值
代码如下:
public static class Compare
{
public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
var type = typeof(T);
var ignoreList = new List<string>(ignore);
var unequalProperties =
from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where !ignoreList.Contains(pi.Name)
let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
let toValue = type.GetProperty(pi.Name).GetValue(to, null)
where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
select selfValue;
return !unequalProperties.Any();
}
return self == to;
}
}
这里是比较:
private void Form1_Load(object sender, EventArgs e)
{
Obj1 obj1 = new Obj1();
Obj1 obj11 = new Obj1();
Obj2 obj2 = new Obj2();
Obj2 obj22 = new Obj2();
obj1.param1 = "1";
obj1.param2 = "2";
obj2.param3 = "3";
obj1.obj2 = obj2;
obj11.param1 = "1";
obj11.param2 = "2";
obj22.param3 = "3";
obj11.obj2 = obj22;
bool res = Compare.PublicInstancePropertiesEqual(obj1, obj11, ("secure"));
}
}
class Obj1
{
public string param1 { get; set; }
public string param2 { get; set; }
public Obj2 obj2 { get; set; }
}
class Obj2
{
public string param3 { get; set; }
public decimal param4 { get; set; }
}
res
returned 值是假的
当您比较 Obj2
的两个实例时,只有当它们是同一对象时它们才相等。
要执行结构相等,您需要递归遍历所有引用类型(即 类),只需直接比较值类型(即默认使用结构相等的结构)。注意 int
等是值类型。
我建议检查覆盖 Equals
, implement IEquatable<T>
, IComparable<T>
等的类型:所有表明类型具有自己的相等定义的迹象。
在您的代码中,obj1.obj2 和 obj11.obj2 的值不同,比较方法使用 Object.Equals 来比较 类 的成员,这就是为什么 Compare.PublicInstancePropertiesEqual 方法 returns 错误。
即:obj1.obj2 = obj2;但是 obj11.obj2 = obj22;
如果您想递归地比较值,您应该替换行
where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
和
where selfValue != toValue && (selfValue == null || !PublicInstancePropertiesEqual(selfValue, toValue, ignore))
我想比较两个 classes 和 return bool 值的数据类型。问题是我的方法不比较 class
的 class 内的值代码如下:
public static class Compare
{
public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
var type = typeof(T);
var ignoreList = new List<string>(ignore);
var unequalProperties =
from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where !ignoreList.Contains(pi.Name)
let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
let toValue = type.GetProperty(pi.Name).GetValue(to, null)
where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
select selfValue;
return !unequalProperties.Any();
}
return self == to;
}
}
这里是比较:
private void Form1_Load(object sender, EventArgs e)
{
Obj1 obj1 = new Obj1();
Obj1 obj11 = new Obj1();
Obj2 obj2 = new Obj2();
Obj2 obj22 = new Obj2();
obj1.param1 = "1";
obj1.param2 = "2";
obj2.param3 = "3";
obj1.obj2 = obj2;
obj11.param1 = "1";
obj11.param2 = "2";
obj22.param3 = "3";
obj11.obj2 = obj22;
bool res = Compare.PublicInstancePropertiesEqual(obj1, obj11, ("secure"));
}
}
class Obj1
{
public string param1 { get; set; }
public string param2 { get; set; }
public Obj2 obj2 { get; set; }
}
class Obj2
{
public string param3 { get; set; }
public decimal param4 { get; set; }
}
res
returned 值是假的当您比较 Obj2
的两个实例时,只有当它们是同一对象时它们才相等。
要执行结构相等,您需要递归遍历所有引用类型(即 类),只需直接比较值类型(即默认使用结构相等的结构)。注意 int
等是值类型。
我建议检查覆盖 Equals
, implement IEquatable<T>
, IComparable<T>
等的类型:所有表明类型具有自己的相等定义的迹象。
在您的代码中,obj1.obj2 和 obj11.obj2 的值不同,比较方法使用 Object.Equals 来比较 类 的成员,这就是为什么 Compare.PublicInstancePropertiesEqual 方法 returns 错误。
即:obj1.obj2 = obj2;但是 obj11.obj2 = obj22;
如果您想递归地比较值,您应该替换行
where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
和
where selfValue != toValue && (selfValue == null || !PublicInstancePropertiesEqual(selfValue, toValue, ignore))