using System.Drawing;
namespace TestApp
{
class Tag
{
private string name;
private int size;
public Tag(string name,int size)
{
this.name = name;
this.size = size;
}
public string Getname()
{
return name;
}
public void Setname(string value)
{
name = value;
}
public int GetSize()
{
return size;
}
public void SetSize(int value)
{
size = value;
}
}
}
static void Main()
{
List<Tag> list = new List<Tag>();
list.Add(new Tag("tag", 100));
list.Add(new Tag("tagger", 1000));
list.Add(new Tag("tager", 500));
list.Add(new Tag("tag", 200));
//List<Tag> listClone = list???这个该怎么写?
}
现在就是想要一个深克隆的例子,比较全的
你这个.tw
我可能。。。
打不开,哈哈,你的10个园豆要泡汤
@小草上飞飞:
Solution A
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class CommonExtensions
{
/// <summary>
/// 深層複製(複製對象須可序列化)
/// </summary>
/// <typeparam name="T">複製對象類別</typeparam>
/// <param name="source">複製對象</param>
/// <returns>複製品</returns>
public static T DeepClone<T>(this T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
if (source != null)
{
using (MemoryStream stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
T clonedSource = (T)formatter.Deserialize(stream);
return clonedSource;
}
}
else
{ return default(T); }
}
}
Solution B
using Newtonsoft.Json;
public static class CommonExtensions
{
/// <summary>
/// 深層複製(需使用Json.Net組件)
/// </summary>
/// <typeparam name="T">複製對象類別</typeparam>
/// <param name="source">複製對象</param>
/// <returns>複製品</returns>
public static T DeepCloneViaJson<T>(this T source)
{
if (source != null)
{
// avoid self reference loop issue
// track object references when serializing and deserializing JSON
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto
};
var serializedObj = JsonConvert.SerializeObject(source, Formatting.Indented, jsonSerializerSettings);
return JsonConvert.DeserializeObject<T>(serializedObj, jsonSerializerSettings);
}
else
{ return default(T); }
}
}
使用
@会长:
唉~還得搬代碼
@RosonJ: 我豆危矣.......
@RosonJ: 啊,也分了我10个园豆,楼主好人
@会长:
你10個,我10個,還行吧
@RosonJ: 你们都那么多豆了,还瓜分我,大清要亡
@小草上飞飞:
豆子還好,我比較想要聲望哈哈哈
@RosonJ: MemoryStream stream = new MemoryStream()
这个流不用 close() 吗?
@小草上飞飞:
用using的方式會自動close
using System;
using System.Collections.Generic;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
List<Tag> list = new List<Tag>
{
new Tag("a", 1),
new Tag("b", 2),
};
var list2 = CloneList(list);
foreach (var item in list)
{
item.Size += 1;
item.Name += "a";
}
Console.WriteLine("list");
PrintList(list);
Console.WriteLine("list2");
PrintList(list2);
Console.ReadLine();
}
private static List<Tag> CloneList(List<Tag> list)
{
List<Tag> newList = new List<Tag>();
foreach (var item in list)
{
newList.Add(item.Clone() as Tag);
}
return newList;
}
private static void PrintList(List<Tag> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
}
class Tag: ICloneable
{
public Tag(string name, int size)
{
Size = size;
Name = name;
}
public int Size { get; set; }
public string Name { get; set; }
public object Clone()
{
return new Tag(Name, Size);
}
public override string ToString()
{
return $"Name:{Name};Szie:{Size}";
}
}
}
另一种呢?就是用[Serializable]
这个标签的
@小草上飞飞: 那是把对象先序列化了然后再反序列化吧,也不错