copy a class, C#

后端 未结 12 1764
失恋的感觉
失恋的感觉 2020-12-03 10:13

Is there a way to copy a class in C#? Something like var dupe = MyClass(original).

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 10:50

    How about with JSON?

    You can get JSON package from : https://www.nuget.org/packages/Newtonsoft.Json/

    using Newtonsoft.Json;
    public static List CopyAll(this List list) {
      List ret = new List();
      string tmpStr = JsonConvert.SerializeObject(list);
      ret = JsonConvert.DeserializeObject>(tmpStr);
      return ret;
    }
    

提交回复
热议问题