How to make a copy of an object in c#
This question already has an answer here:
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>(T other)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, other);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
Additional Info: (you can also read this article from MSDN)
Shallow copying is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed; for a reference type, the reference is copied but the referred object is not; therefore the original object and its clone refer to the same object.
Deep copy is creating a new object and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed.
You can use MemberwiseClone
obj myobj2 = (obj)myobj.MemberwiseClone();
The copy is a shallow copy which means the reference properties in the clone are pointing to the same values as the original object but that shouldn't be an issue in your case as the properties in obj
are of value types.
If you own the source code, you can also implement ICloneable
It sounds like your looking for an easy way to loosely couple your objects, but maintain the ability to allow the values to be changed when needed.
One way of passing values between classes can be like this.
public class Folders
{
public string Source { get; private set; }
public string Target { get; private set; }
public Folders(string source, string target)
{
if (String.IsEmptyOrNull(source) && String.IsEmptyOrNull(target))
{
throw new Exception("Null Parameter");
}
else
{
Source = source;
Target = target;
}
}
}
So we've created a particular object called Folders
. This can now be called in a second class file like this:
public bool CopyDirectory(string _source, string _destination)
{
var dirStack = new Stack<Folders>();
dirStack.Push(new Folders(_source, _destination));
// Perform stuff related to this method.
}
So in our second class we are passing our methods parameters to this original class, which will set our values and allow us to manipulate them.
We have another way, so in a separate class lets say we wanted to use our Constructor to pass the variables. This will allow an initial reference then the ability to actually deviate if we choose to.
public class DirectoryManagement
{
private Folders _folder { get; private set; }
public DirectoryManagement(Folders folder)
{
_folder = folder;
}
}
So when we utilize this manner, we are actually ensuring the parameters initially passed to our Folders
class are passed into our new class DirectoryManagement
. Which would allow us to either link these values to other variables, use these values, or whatever our needs are.
I'm not sure if this approach or the approach listed is your ideal solution. But I thought I'd present another manner of passing and using existing variables. Also some details on MemberwiseClone
can be found here.
上一篇: 使用C#将属性值复制到另一个对象
下一篇: 如何在c#中创建一个对象的副本