Possible Duplicate: Cloning objects in C# I have an object created in c# say Object1. I need to temporarily back up this object to another object say ObjectOriginal. However if I do ObjectOriginal = Object1, any changes to Object 1 affect ObjectOriginal. How do I go about this? It all depends on what Object1 actually is, ie is it a DataTable, a String, or something else entirely? By w
可能重复: 在C#中克隆对象 我有一个在c#中创建的对象,比如Object1。 我需要暂时将这个对象备份到另一个对象,如ObjectOriginal。 但是,如果我做ObjectOriginal = Object1,对对象1的任何更改都会影响ObjectOriginal。 我如何去做这件事? 这一切都取决于Object1实际是什么,即它是一个DataTable,一个字符串,或者其他什么东西? 通过写作: object Object1 = new Thing(); object Object2 = Object1; 您将获
This question already has an answer here: Deep cloning objects 39 answers A simple way is to serialize the object, it will make a deep copy of the source object. http://www.codeproject.com/Articles/23832/Implementing-Deep-Cloning-via-Serializing-objects
这个问题在这里已经有了答案: 深入克隆对象39个答案 一个简单的方法是序列化对象,它将创建源对象的深层副本。 http://www.codeproject.com/Articles/23832/Implementing-Deep-Cloning-via-Serializing-objects
This question already has an answer here: Deep cloning objects 39 answers 使用这样的东西public object Clone() { using (var memStream = new MemoryStream()) { var binaryFormatter = new BinaryFormatter( null, new StreamingContext(StreamingContextStates.Clone)); binaryFormatter.Serialize(memStream, this); memStream.Seek(0, SeekOrigin.Begin);
这个问题在这里已经有了答案: 深入克隆对象39个答案 使用这样的东西public object Clone() { using (var memStream = new MemoryStream()) { var binaryFormatter = new BinaryFormatter( null, new StreamingContext(StreamingContextStates.Clone)); binaryFormatter.Serialize(memStream, this); memStream.Seek(0, SeekOrigin.Begin); return binaryForm
Possible Duplicate: Cloning objects in C# I have a class with properties and some of them are reference types (instances of other classes) themselves ( CookieContainer ). I want to have a exact copy of this class so any change to previous version would not affect this new instance. Is there a general solution for this kind of problems or I should do it manually ? You need to deep copy t
可能重复: 在C#中克隆对象 我有一个类的属性,其中一些是引用类型(其他类的实例)本身( CookieContainer )。 我想要这个类的确切副本,所以对以前版本的任何更改都不会影响这个新实例。 有这种问题的一般解决方案,或者我应该手动做? 您需要将该对象深层复制到另一个对象。 对此有许多方法,但序列化一个对象并将该数据反序列化到另一个对象中是实现此目的的一个非常快速的技巧。 在这里看到:你如何在.NET中
This question already has an answer here: Deep cloning objects 39 answers public static void CopyPropertiesTo<T, TU>(this T source, TU dest) { var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList(); var destProps = typeof(TU).GetProperties() .Where(x => x.CanWrite) .ToList(); foreach (var sourceProp in sourceProps) {
这个问题在这里已经有了答案: 深入克隆对象39个答案 public static void CopyPropertiesTo<T, TU>(this T source, TU dest) { var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList(); var destProps = typeof(TU).GetProperties() .Where(x => x.CanWrite) .ToList(); foreach (var sourceProp in sourceProps) { if (destProps.Any(x
This question already has an answer here: Deep cloning objects 39 answers Properties in your object are value types and you can use shallow copy in such sutuation like that: obj myobj2 = (obj)myobj.MemberwiseClone(); But in other situations like if any members are reference types, then you need Deep Copy. You can get deep copy of object using this method: public static T DeepCopy<T>
这个问题在这里已经有了答案: 深入克隆对象39个答案 你的对象中的属性是值类型,你可以像这样使用浅色副本: obj myobj2 = (obj)myobj.MemberwiseClone(); 但在其他情况下,如果有任何成员是引用类型,则需要使用Deep Copy。 您可以使用此方法获得对象的深层副本: public static T DeepCopy<T>(T other) { using (MemoryStream ms = new MemoryStream()) { BinaryFormatter formatter = new Bina
What is my error when trying to decrypt? When calling DecrypToken shown this message: An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: The input stream is not a valid binary format. [EDIT] Data now converted data back from the hex format. Key and IV now is saved to reuse. Now getting this er
试图解密时我的错误是什么? 当调用DecrypToken显示此消息时: mscorlib.dll中发生未处理的类型为“System.Runtime.Serialization.SerializationException”的异常 附加信息:输入流不是有效的二进制格式。 [编辑] 数据现在将数据从十六进制格式转换回来。 Key和IV现在被保存以重用。 现在得到这个错误: mscorlib.dll中发生未处理的类型为“System.Runtime.Serialization.SerializationException”的异常 其他信
MSDN gives this example of a deep copy (http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx) public class Person { public int Age; public string Name; public IdInfo IdInfo; public Person ShallowCopy() { return (Person)this.MemberwiseClone(); } public Person DeepCopy() { Person other = (Person) this.MemberwiseClone();
MSDN提供了深层复制的示例(http://msdn.microsoft.com/zh-cn/library/system.object.memberwiseclone.aspx) public class Person { public int Age; public string Name; public IdInfo IdInfo; public Person ShallowCopy() { return (Person)this.MemberwiseClone(); } public Person DeepCopy() { Person other = (Person) this.MemberwiseClone(); other.IdInfo
I need a semi-shallow copy of an object. Under my original design I used MemberwiseClone to catch all the simple stuff and then I specifically copied the classes to the extent that they needed to be copied. (Some of them are inherently static and most of the rest are containers holding static items.) I didn't like the long list of copies but there's no way around that. Now, however, I
我需要一个对象的半浅拷贝。 在我最初的设计中,我使用MemberwiseClone捕捉所有简单的东西,然后专门复制这些类到需要复制的程度。 (其中一些本质上是静态的,其余大部分都是容纳静态物品的容器。)我不喜欢长长的副本列表,但是没有办法解决这个问题。 但是,现在我发现自己需要创建一个后代对象 - 我现在是否必须返回并复制以前使用MemberwiseClone复制的所有这些字段? 或者我错过了一些更好的解决方法? 我发现克隆
Possible Duplicate: What is the difference between a deep copy and a shallow copy? I saw this today here: http://msdn.microsoft.com/en-us/library/system.web.routing.route.aspx and one of the member functions was: "MemberwiseClone - Creates a shallow copy of the current Object. (Inherited from Object.)" So whats a "shallow copy" verse a ... "Deep copy"? Shal
可能重复: 深拷贝和浅拷贝之间有什么区别? 我今天在这里看到了这个:http://msdn.microsoft.com/en-us/library/system.web.routing.route.aspx,其中一个成员函数是: “MemberwiseClone - 创建当前Object的浅表副本。(从Object继承。)” 那么什么是“浅拷贝”呢......“深拷贝”呢? 浅拷贝仅替换对象当前级别的属性,这意味着如果您将对象作为属性,它将与原始对象具有相同的引用。 如果你的属性当然是值类型或基元