c#比较两个对象模型中的数据 [英] c# compare the data in two object models

查看:292
本文介绍了c#比较两个对象模型中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框,当生成它被一个对象模型中的数据填充。此时,数据被复制并存储在备份对象模型中。当用户完成更改,并单击确定以关闭对话框,我需要一个快速的方法来比较备份对象模型与实时模型 - 如果有任何更改,我可以创建一个新的撤消状态。 / p>

如果可能,我不想在对象模型中为每个类写入比较函数。



如果我将两个对象模型序列化,并且它们是相同的,但是存储在不同的内存位置,它们是否相等?有没有一些简单的方法来比较两个序列化对象模型?

解决方案

我没有打扰哈希字符串,二进制序列化工程奇迹。当对话框打开时序列化对象模型。

  BinaryFormatter formatter = new BinaryFormatter 
m_backupStream = new MemoryStream();
formatter.Serialize(m_backupStream,m_objectModel);

然后如果用户使用可用控件添加到对象模型当对话框关闭时,您可以与一个新的序列化比较 - 这对我来说是我如何决定是否需要撤消状态。

  BinaryFormatter formatter = new BinaryFormatter(); 
MemoryStream liveStream = new MemoryStream();
formatter.Serialize(liveStream,m_objectModel);
byte [] streamOneBytes = liveStream.ToArray();
byte [] streamTwoBytes = m_backupStream.ToArray();
if(!CompareArrays(streamOneBytes,streamTwoBytes))
AddUndoState();

并且比较数组函数,任何人都需要它 - 不是最好的方式比较两个数组

  private bool CompareArrays(byte [] a,byte [] b)
{
if a.Length!= b.Length)
return false;

for(int i = 0; i {
if(a [i]!= b [i])
return false;
}
return true;
}


I have a dialog, when spawned it gets populated with the data in an object model. At this point the data is copied and stored in a "backup" object model. When the user has finished making their changes, and click "ok" to dismiss the dialog, I need a quick way of comparing the backup object model with the live one - if anything is changed I can create the user a new undo state.

I don't want to have to go and write comparison function for every single class in the object model if possible.

If I serialised both object models and they were identical but stored in different memory locations would they be equal? Does some simple way exist to compare two serialised object models?

解决方案

I didn't bother with a hash string but just a straight Binary serialisation works wonders. When the dialog opens serialise the object model.

BinaryFormatter formatter = new BinaryFormatter();
m_backupStream = new MemoryStream();
formatter.Serialize(m_backupStream,m_objectModel);

Then if the user adds to the object model using available controls (or not). When the dialog closes you can compare to the original serialisation with a new one - this for me is how i decide whether or not an Undo state is required.

BinaryFormatter formatter = new BinaryFormatter();
MemoryStream liveStream = new MemoryStream();
formatter.Serialize(liveStream,m_objectModel);
byte[] streamOneBytes = liveStream.ToArray();
byte[] streamTwoBytes = m_backupStream.ToArray();
if(!CompareArrays(streamOneBytes, streamTwoBytes))
    AddUndoState();

And the compare arrays function incase anybody needs it - prob not the best way of comparing two arrays im sure.

private bool CompareArrays(byte[] a, byte[] b)
{
    if (a.Length != b.Length)
       return false;

    for (int i = 0; i < a.Length;i++)
    {
       if (a[i] != b[i])
        return false;
    }
    return true;
}

这篇关于c#比较两个对象模型中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆