As we all know, the storage of variables in C # is divided into value types and reference types, and value types and reference types in numerical changes are not the same result, value types we can easily achieve a copy of the values, then the reference type, there is a certain difficulty in the object copy.
Let me start with a classic example.private voidDochange (String a)
{
intb = A;
b = "2";
System.Console.WriteLine (b);
System.Console.WriteLine (a);
When I call the above function Dochange ("1"), what is the result of the output? A lot of Big Brother drove to me to ask this question, must be angry to squalling, hehe, very simple, the output is: 21 then let's look at one of the following examplesPublic classdata{Pubic StringKey= "1"}private voidDochange (DataA
{
Datab = A;
B.key = "2";
System.Console.WriteLine (B.key.tostring ());
System.Console.WriteLine (A.key.tostring ());
We call Dochange again, what is the output of the new data? Some people say: 21 If you also think so, then you are wrong, hehe! The correct result is ... 22 Why would that be? A lot of people must be surprised that the reason for this problem is that the value type and the reference type, the first value functionstring itself is a value type, he is in the storage of time, is directly open up a storage space, and the second data type in storage, in fact, through the pointer to the variable and its storage space linked together, when declaring data b=a, A pointer to B points to the storage location pointed to by a pointer, and when the b.key= "2" is assigned, the B.key point to the storage space is assigned to "2", this time because A and B pointers point to the same storage space, so the value of A.key and B.key becomes 2. so the problem arises, how can b and a not change at the same time? Someone will tell me, you can write like this! private void Dochange (data a)
{
Data B = new data();b = A;
B.key = "2";
System.Console.WriteLine (B.key.tostring ());
System.Console.WriteLine (A.key.tostring ());
}in the case of new, the system will open up two different storage spaces for A and B, so there will be no problem. In fact, when you new, it is true that A and B have a different storage location, you can when you b=a, in fact, the pointer to the B point to a storage location, and the storage location of B is idle, before long, C # garbage collection mechanism will be B's storage space for recycling. isn't this bad, when I use a complex object, how can I make one object equal to another object, and the property of one object will not change after the property value of one of the objects changes? in C #, there are write system objects that provide a clone method, but for user-defined objects There is no such method, we want to implement the cloning operation, we must manually to facilitate each property, and then assign a value to the property, that is, the following method. private void Dochange (data a)
{
Data B = new data();
B.key = A.key;B.key = "2";
System.Console.WriteLine (B.key.tostring ());
System.Console.WriteLine (A.key.tostring ());
}This is fine for objects with very few properties, but it is quite cumbersome to manipulate objects with many properties, so you can use the mechanism of reflection to assign values to each property, as follows.Public static voidCopyvalue (object Origin,object target)
{
System.reflection.propertyinfo[] Properties = (target. GetType ()). GetProperties ();
system.reflection.fieldinfo[] fields = (origin. GetType ()). GetFields ();
for(int i=0; i< fields. Length; i++)
{
for(int j=0; j< properties. Length; J + +)
{
if (Fields[i]. Name = = Properties[j]. Name && Properties[j]. CanWrite)
{
PROPERTIES[J]. SetValue (Target,fields[i]. GetValue (origin), null);
}
}
}
Haha, so far, the problem has finally been solved. Borrowed from: http://blog.sina.com.cn/s/blog_4c8a4e5901009lya.html
C # Object Copy problem = equivalent to shallow copy