如何执行对象的深层副本没有标记为可序列(在C#)? [英] How to perform a deep copy of an object not marked as serializable (in C#)?

查看:344
本文介绍了如何执行对象的深层副本没有标记为可序列(在C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C#中剪贴板堆栈。剪贴板中的数据存储在 System.Windows.Forms.DataObject 的对象。我想直接存储每个剪贴板条目(的IDataObject )的泛型列表。由于这样的位图存储,我想我需要进行深拷贝第一之前,我把它添加到列表(似乎)。



我试图使用二进制序列化(见下文),以创建一个深层复制,但是因为 System.Windows.Forms.DataObject 未标记为可序列化的序列化步骤失败。 ?任何想法

 公共IDataObject的GetClipboardData()
{
的MemoryStream MemoryStream的=新的MemoryStream();
的BinaryFormatter的BinaryFormatter =新的BinaryFormatter();
binaryFormatter.Serialize(的MemoryStream,Clipboard.GetDataObject());
memoryStream.Position = 0;
回报(IDataObject的)binaryFormatter.Deserialize(MemoryStream的);
}


解决方案

查找用于可序列化码头并找到有关序列佣工的东西。您可以使用.NET框架包裹位图自己的序列化代码的集成。


I am attempting to create a Clipboard stack in C#. Clipboard data is stored in System.Windows.Forms.DataObject objects. I wanted to store each clipboard entry (IDataObject) directly in a Generic list. Due to the way Bitmaps (seem to be) stored I am thinking I need to perform a deep copy first before I add it to the list.

I attempted to use Binary serialization (see below) to create a deep copy but since System.Windows.Forms.DataObject is not marked as serializable the serialization step fails. Any ideas?

public IDataObject GetClipboardData()
{
    MemoryStream memoryStream = new MemoryStream();
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(memoryStream, Clipboard.GetDataObject());
    memoryStream.Position = 0;
    return (IDataObject) binaryFormatter.Deserialize(memoryStream);
}

解决方案

Look up the docks for Serializable and find the stuff about serialization helpers. You can wrap the bitmap in your own serialization code the integrates with the .net framework.

这篇关于如何执行对象的深层副本没有标记为可序列(在C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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