Cloning anonymous types?

What I want to do is pass IEnumerable to the method and return an IEnumerable instance to it. However, I want each collection to be a copy, and not just a copy that is a copy.

Example:

// some random data from L2S
var data = (from p in sourceData
           select new
           {
               a = 1,
               b = "hello",
           }).ToArray();

var dataCopy = data.Copy(); // somehow i want 'dataCopy' to be a deep copy of 'data'

foreach (var d in dataCopy)
{
    // alter the item in the collection somehow
    d.b = d.b + "1";
}

// combine both sets of data
var finalData = data.Union(dataCopy);

Thus, the collection 'finalData' has twice as many elements as 'data' or 'dataCopy'. Thus, all parameters β€œb” in β€œdataCopy” are added to the end, but since they still refer to objects in β€œdata”, all parameters β€œb” in β€œdata” also have β€œ1” added to the end.

, "" , BinaryFormatter, . Activator.CreateInstance, .

, , , Serializable, , , , ...

, - ? :

var data = (from p in sourceData
           select new SomeSortOfAnonymousTypeReplacement(new
           {
               a = 1,
               b = "hello",
           })).ToArray();

?

+3
1

, , , . # . , , (, ). .

, , - , . , . , , , .

, ? .

+1

Source: https://habr.com/ru/post/1715296/


All Articles