当前位置:网站首页>C#使用Objects Comparer进行对象比较
C#使用Objects Comparer进行对象比较
2022-07-21 13:19:00 【biyusr】
介绍
Objects Comparer是用于对象比较的工具,c#常见的数据结构都是可以用这个三方库进行对比,比较复杂的对象也是可以比较的。
简而言之,Objects Comparer 是一个对象到对象的比较器,
它允许逐个成员递归得比较对象,并为某些属性、字段或类型定义自定义比较规则。
安装
nuget搜索ObjectsComparer
使用
首先我们定义一个简单类
public class UserInfomation
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
然后实例化两个UserInfomation对象并赋不同的值
然后我们实例化 ObjectsComparer.Comparer比较器
var comparer1 = new ObjectsComparer.Comparer<UserInfomation>();
然后我们将实例化的两个对象传入到 ObjectsComparer.Comparer 方法中
IEnumerable<Difference> differences1;
var isEqual1 = comparer1.Compare(userInfomationOld, userInfomationNew, out differences1);
然后通过返回值判断对象是否一致,如果不一致可以通过differences1获取到不一致的值
查看输出 能够知道实例化的两个对象是age属性的值不一样
那我们再试试List<T>类型的
List<UserInfomation> lstUserInfomationsOld=new List<UserInfomation>();
for (int i = 0; i < 3; i++)
{
UserInfomation user=new UserInfomation();
user.Name = "张三";
user.Age = 30;
user.Sex = "男";
lstUserInfomationsOld.Add(user);
}
List<UserInfomation> lstUserInfomationsNew = new List<UserInfomation>();
for (int i = 0; i < 2; i++)
{
UserInfomation user = new UserInfomation();
user.Name = "李四";
user.Age = 30;
user.Sex = "男";
lstUserInfomationsNew.Add(user);
}
var comparer = new ObjectsComparer.Comparer<List<UserInfomation>>();
IEnumerable<Difference> differences;
var isEqual = comparer.Compare(lstUserInfomationsNew, lstUserInfomationsOld, out differences);
string differencesMsg = string.Join(Environment.NewLine, differences);
Console.WriteLine(differencesMsg);
查看输出能够看出是数量不一致的问题
应用场景
像做过.net客户端开发的人都知道,我们在维护一些基础数据的时候都避免不了要编辑数据!
有的时候我们打开编辑页面,实际未修改数据,再去点击保存按钮要不一个一个字段去对比有没有修改数据
要不就直接暴力处理, 不校验有没有修改数据,直接调用update接口
那么我们的Objects Comparer就派上用场了
我们首先封装一个BaseForm
然后在基类控件中 封装一个比较方法
protected Result ComPare<T>(T t, T s)
{
Result result =new Result();
var comparer = new ObjectsComparer.Comparer<T>();
IEnumerable<Difference> differences;
bool isEqual = comparer.Compare(t, s, out differences);
result.IsEqual = isEqual;
if (!isEqual)
{
string differencesMsg = string.Join(Environment.NewLine, differences);
result.Msg=differencesMsg;
}
return result;
}
public class Result
{
public bool IsEqual { get; set; }
public string Msg { get; set; }
我们在打开编辑页面的时候会加载当前页面的数据
这时候 我们可以获取到未编辑之前的数据将它设置为全局变量
然后保存的时候我们可以获取到编辑之后的对象
这时候我们再去调用基类的比较方法
获取两个对象之间值是否有改变,如果没有改变,我们就给出"数据未修改,请问是否关闭窗体“等提示
public partial class MainFrm : BaseForm
{
Test _testOld;
public MainFrm()
{
InitializeComponent();
_testOld = LoadData();
txtName.Text= _testOld.Name;
txtAge.Text = _testOld.Age.ToString();
txtSex.Text = _testOld.Sex;
}
private Test LoadData()
{
Test test = new Test();
test.Name = "张三";
test.Age = 30;
test.Sex = "男";
return test;
}
private void uiButton1_Click(object sender, EventArgs e)
{
Test test=new Test();
test.Name =txtName.Text;
test.Age =int.Parse( txtAge.Text);
test.Sex=txtSex.Text;
Result result= ComPare(_testOld, test);
if (result.IsEqual)
{
MessageBox.Show("数据未修改");
return;
}
//然后再写保存逻辑
MessageBox.Show("保存成功");
}
}
public class Test
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
当然还有很多应用场景,我只是分享我常使用的场景罢了。
最后希望各位neter分享更多有用的工具,共同改善net环境!
边栏推荐
- Share an introductory guide to canvas
- 【读书会第13期】+第二章 视频文件的封装格式
- 表情包小程序,副业日入400+
- Weilai "city" exploration road
- js正则校验只能存在数字和小数点
- Detailed explanation of file upload basis
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (6)
- FTP服务与配置
- Exclusive interview with sportlive Li Ying: how to use Web3 to promote sports IP to expand the "new business territory"
- Write bootloader from 0 - bootloader relocates app
猜你喜欢
Solution of access denied for user 'root' @ 'localhost' (using password: yes)
Some practical debugging skills of Chrome browser in Windows system
How to pre train multimodal models using multi type data?
Exclusive interview with sportlive Li Ying: how to use Web3 to promote sports IP to expand the "new business territory"
【微信小程序】WXSS和全局、页面配置入门砖
ES6 从入门到精通 # 02:let 和 const 命令
面试突击66:请求转发和请求重定向有什么区别?
Win11如何增强麦克风?Win11增强麦克风的设置
低代码平台搭建跨部门沟通系统案例分析
pip下载包时出现不适配导致无法下载安装包:error: subprocess-exited-with-error;error: metadata-generation-failed;
随机推荐
Tensorflow入门教程(三十四)——常用两类图像分割损失函数
Introduction and implementation of audio and video ring buffer
面试突击66:请求转发和请求重定向有什么区别?
LeetCode·每日一題·814.二叉樹剪枝·遞歸
Web red alert, how to turn the audio stored locally into a playable state
[并发编程基础]-集合的线程安全
Operation of Py file
一文深入浅出理解国产开源木兰许可系列协议
strcmp()&nbsp;- 比较字符串
长期的远程工作面临的几个问题和持续改进的组织自动化
Vulnerability analysis hevd-0x6 UninitializedStackVariable[win7x86]
ES6 从入门到精通 # 02:let 和 const 命令
unity的静态设置以及烘培属性
The landing of tdengine in the GPS and AIS scheduling of Zhongtian steel
LeetCode·每日一题·814.二叉树剪枝·递归
【链表及其经典问题】
牛客刷题篇
如何使用多类型数据预训练多模态模型?
判断一个人靠不靠谱,就看这3点
2022.7.9 summer vacation personal training 1-b.how old are you Mr. string