本文介绍了比较数据类型,C#中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想比较两个类的数据类型并返回bool值。问题是我的方法不比较类中的类值 这是代码: public static class 比较 { public static bool PublicInstancePropertiesEqual< T>( this T self,T to, params string [] ignore)其中 T: class { if (self!= null && to != null ) { var type = typeof运算(T); var ignoreList = new List< string>(ignore); var unequalProperties = 来自 pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) where !ignoreList.Contains(pi.Name) 让 selfValue = type.GetProperty(pi.Name).GetValue(self, null ) 让 toValue = type.GetProperty(pi.Name).GetValue(to, null ) 其中 selfValue!= toValue&& (selfValue == null ||!PublicInstancePropertiesEqual(selfValue,toValue,ignore)) 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 = 4; obj11.obj2 = obj22; bool res = Compare.PublicInstancePropertiesEqual(obj1,obj11,( 安全)); } } 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 ; } } 即使参数值发生变化,返回值也为真。 div class =h2_lin>解决方案 I'd like to compare the data types of two classes and return bool values. The problem is my method doesn't compare values inside class of a classHere is the code: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 || !PublicInstancePropertiesEqual(selfValue, toValue, ignore)) select selfValue; return !unequalProperties.Any(); } return self == to; }}And here is the comparison: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 = "4"; 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; }}The returned value is res is true even when the values of the parameters change. 解决方案 这篇关于比较数据类型,C#中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 10:01