- 浏览: 427885 次
- 性别:
- 来自: 唐山
文章分类
最新评论
-
hautbbs:
谢谢分享!
ASP.NET 导出Excel 和csv -
hautbbs:
感谢分享!
ASP.NET 导出Excel乱码的终极解决 -
wyf:
zcl920 写道只能说 看不懂。要发就发全 取一段出来 有什 ...
图片上绘制文字换行处理 -
zcl920:
只能说 看不懂。要发就发全 取一段出来 有什么用。
图片上绘制文字换行处理 -
380086154:
有用,谢谢。
js比较日期
深度Clone 一个快速,肮脏的方法(Deep Clone of a business object: the quick and dirty way)
- 博客分类:
- ASP.NET
原文出处:http://www.primordialcode.com/index.php/2008/10/18/deep-clone-business-object-quick-dirty/
I was implementing the IEditableObject interface for some entity classes in my current project and I needed a quick and dirty way to do the deep clone of an object, since my entity classes are shared between WCF, WPF and Silverlight (yes I’m one of the guys so crazy to build multi-target applications) I needed a method that could work in all the environments.
In WPF and WCF you can rely on the binary formatter and the serialization (entity classes have to be marked with the Serializable attribute), and use code like this:
1: public static partial class Helpers 2: { 3: public static T DeepClone<T>(this T obj) 4: { 5: T cloned = default(T); 6: var serializer = new BinaryFormatter(); 7: using (var ms = new MemoryStream()) 8: { 9: serializer.Serialize(ms, obj); 10: ms.Position = 0; 11: cloned = (T)serializer.Deserialize(ms); 12: } 13: return cloned; 14: } 15: }
In Silverlight you can write something similar and use the DataContractSerializer and the xml serialization:
1: public static partial class Helpers 2: { 3: public static T DeepClone<T>(this T obj) 4: { 5: T cloned = default(T); 6: var serializer = new DataContractSerializer(typeof(T)); 7: using (var ms = new MemoryStream()) 8: { 9: serializer.WriteObject(ms, obj); 10: ms.Position = 0; 11: cloned = (T)serializer.ReadObject(ms); 12: } 13: return cloned; 14: } 15: }
Due to the restriction of Silverlight the DataContractSerializer has some limitations and they depends if you are using or not the DataContract + DataMember attributes:
With no attributes you can only serialize types that have a default public constructor with no arguments and all public fields/properties, for example something like:
1: public class TestEntityNoAttributes 2: { 3: public string PublicField; 4: 5: public TestEntityNoAttributes() 6: { 7: } 8: 9: public TestEntityNoAttributes(string pub) 10: { 11: PublicField = pub; 12: } 13: }
Using DataContract + DataMember attribute you can choose what to serialize, you do not need the default constructor anymore, but still you are limited to public fields only...unless you use the InternalsVisibleTo attribute and convert the protected and private fields you want to serialize to internal, the class will look like:
1: [DataContract] 2: public class TestEntityAttributes 3: { 4: [DataMember] 5: public string PublicField; 6: 7: /// <summary> 8: /// changed from protected to internal 9: /// it has to be internal to allow serialization and be used with InternalsVisibleTo assembly attribute 10: /// </summary> 11: [DataMember] 12: internal string _ProtectedField; 13: public string ProtectedField 14: { 15: get { return _ProtectedField; } 16: } 17: 18: /// <summary> 19: /// changed from private to internal 20: /// it has to be internal to allow serialization and be used with InternalsVisibleTo assembly attribute 21: /// </summary> 22: [DataMember] 23: internal string _PrivateField; 24: public string PrivateField 25: { 26: get { return _PrivateField; } 27: } 28: 29: public TestEntityAttributes(string pub, string pro, string pri) 30: { 31: PublicField = pub; 32: _ProtectedField = pro; 33: _PrivateField = pri; 34: } 35: }
plus you have to tag the assembly that holds the entities and make its internal members visibile to the serializer, since DataContractSerializer resides in the System.Runtime.Serialization you have to mark the assembly with:
1: //needed to allow the serializer to access internal members 2: [assembly: InternalsVisibleTo("System.Runtime.Serialization")]
Then using the Silverlight Unit Testing Framework from Jeff Wilcox, you can write a couple of tests like the following ones to verify that it works.
1: [TestMethod] 2: public void TestEntityNoAttributeDeepClone() 3: { 4: TestEntityNoAttributes e = new TestEntityNoAttributes("1"); 5: TestEntityNoAttributes eCloned = e.DeepClone(); 6: Assert.AreEqual(e.PublicField, eCloned.PublicField); 7: } 8: 9: [TestMethod] 10: public void TestEntityAttributeDeepClone() 11: { 12: TestEntityAttributes e = new TestEntityAttributes("1", "2", "3"); 13: TestEntityAttributes eCloned = e.DeepClone(); 14: Assert.AreEqual(e.PublicField, eCloned.PublicField); 15: Assert.AreEqual(e.ProtectedField, eCloned.ProtectedField); 16: Assert.AreEqual(e.PrivateField, eCloned.PrivateField); 17: }
Naturally using both those methods you incur in performance penalty...but hey...this is the quick and dirty way after all.
发表评论
-
登录时记住用户名密码的实现方式
2019-06-12 15:11 2982登录的时候记住用户 ... -
CAS 实现单点登录 .NET MVC
2016-05-24 17:14 1087http://www.cnblogs.com/woxpp/p ... -
.NET开发邮件发送功能的全面教程(含邮件组件源码)
2015-03-31 09:43 1116原文地址:http://www.cnblogs.com/he ... -
开发Web组合
2015-01-04 11:39 5891、数据库操作 ORM-Dapper 2、前台界面布局采 ... -
基于 Bootstrap 构建的网站
2014-12-14 14:12 613文档,下载地址:http://v3.bootcss.com ... -
iis8 默认不支持svc解决方法
2014-09-18 18:57 737以下内容对于使用WIN2012 部署V9的时候使用。 ... -
C# 连接Oracle(利用ODP.net,不安装oracle客户端)
2014-07-11 09:37 1671C# 连接Oracle(利用ODP.net,不安装oracl ... -
C# Attribute 特性,过期特性
2014-05-27 15:18 1791通过下列过程将属性应用到代码元素。 通过从 .NE ... -
.NET画实时直方图
2011-12-30 09:37 885using System; using System.Col ... -
设置combobx选中项
2011-12-21 15:20 1012cbRole.SelectedIndex = cbRole.I ... -
文档树状结构化目录管理方法
2011-12-20 09:50 2117本文适用于附件(各类文档、图片和压缩包等,下同)比较多的 ... -
.StringTemplate替换模板
2011-11-03 10:19 1222官方下载 www.StringTemplate. ... -
WCF-IErrorHandler
2011-10-11 16:30 1019使用 IErrorHandler 接口,我们可以更深入地 ... -
ADODB.Stream instead of Scripting.FileSystemObject.
2011-07-04 08:55 1225In a Silverlight 4 OOB App (eve ... -
Scripting.FileSystemObject对象的详细技巧指南
2011-07-03 23:39 1023Scripting.FileSystemObject对象的 ... -
Stream 和 byte[] 之间的转换
2011-07-02 16:52 1049/* - - - - - - - - - - - - - ... -
常用正则表达式
2011-06-15 20:17 770正则表达式用于字符 ... -
DynamicMethod 类
2011-05-11 22:51 1130public delegate String MyMetho ... -
一个通用的快速反射方法(A General Fast Method Invoker)
2011-04-13 22:01 1499普通反射方法 MethodInfo methodIn ... -
C#操作IIS(转)可以写一个工具自己配置网站
2011-03-24 21:08 2249using System; using System.Dir ...
相关推荐
“the emulator is JLink-Clone, the segger software only support orginal segger device” 然后闪退,IDE崩溃关闭! 解决方案: 1.升级压缩包里的固件(该固件将SN修改为默认的-1)。 2.进入J-Link Commander,...
arcgis Pro深度学习环境部署教程,clone一个名字为plam-detection的工程环境;clone的位置在C:\Users\admin\AppData\Local\ESRI\conda\envs可以去在Pro的project下切换环境。(安装完Pro后默认的conda的环境位置:C:...
This book is a hands-on, real-world guide to building and implementing a robust Oracle E-Business Suite System using the new Release, EBS 12.2. It explains the rationale for using an Oracle E-business...
An effective and efficient assembly code clone search engine can greatly reduce the effort of this process, since it can identify the cloned parts that have been previously analyzed. The assembly ...
A tour of git: the basics A tour of git: the basics 2.0 Copyright This document is a modified version of a document originally titled "Distributed revision control with Mercurial" and originally ...
对象克隆ES5的深克隆和比较概要// just ... clone ( src ) ; // shallow copylog ( Object . is ( src , dst ) ) ; // falselog ( Object . equals ( src , dst ) ) ; // truedst . lang . push ( 'javascript' ) ;log
c++写的一个github快速下载器,clone速度可达10M/s c++写的一个github快速下载器,clone速度可达10M/s c++写的一个github快速下载器,clone速度可达10M/s c++写的一个github快速下载器,clone速度可达10M/s c++...
Object类中的clone方法clone()方法的作用克隆方法用于创建对象的拷贝,为了使用clone方法,类必须实现java.lang.Cloneable接口
使用网上流传的一个程序实现pdf截图为png,需要使用Imagic扩展。在windows下安装完后提示: Fatal error: Trying to clone an uncloneable object of class Imagick in C:\www\hx\pdf_to_png.php on line 17 使用IIS...
详细的描述了Java中 clone方法使用
Jlink-clone解决办法,替换文件
The book starts by building a clone of the classical snake game where you will learn how to open a window and render a basic sprite, write well-structured code to implement the design of the game, and...
一个在公司代理后面代理API请求的快速项目 入门 克隆此存储库: git clone https://github.com/maxence-lefebvre/pokeapi-proxy-quick-dirty.git 配置 您应该通过在此文件夹的根目录下创建一个.env文件来指定代理...
用例主要用例是创建一个值与原始对象相等但没有共享引用的对象,该共享引用会使修改原始对象导致克隆中的更改,例如以下示例: const example = { deep : { inside : 'value' } } ;const shallow = { ... example } ...
JavaScript中并没有直接提供对象复制(Object Clone)的方法。因此下面的代码中改变对象b的时候,也就改变了对象a。 a = {k1:1, k2:2, k3:3}; b = a; b.k2 = 4; 如果只想改变b而保持a不变,就需要对对象a进行复制。 ...
47丨原型模式:如何最快速地clone一个HashMap散列表?1
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ...
More complex inline functions may also be put in a .h file for the convenience of the implementer and callers, though if this makes the .h file too unwieldy you can instead put that code in a ...
解决git clone时报错问题:sign_and_send_pubkey: signing failed: agent refused operation
var clone = require ( '@dmail/object-clone' ) ; var a = { name : 'a' } ; var b = clone ( a ) ; b . name ; // 'a' // of course b != a 它克隆得很深 var a = { list : [ { name : 'first' } ] } ; var...