use newtonsoft json to clone object
C#
// using Newtonsoft.Json;
// This sample is from the StackOverflow response (source cited) with
// a couple of modifications
public static T Clone<T>(T source)
{
var jsonSettings = new Newtonsoft.Json.JsonSerializerSettings() {
// Use this option to ignore reference looping option
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
// Use this option when properties use an Interface as the type
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
};
var serialized = JsonConvert.SerializeObject(source, jsonSettings);
return JsonConvert.DeserializeObject<T>(serialized, jsonSettings);
}
/* For System Extension Method */
public static class SystemExtension
{
public static T Clone<T>(this T source)
{
var jsonSettings = new Newtonsoft.Json.JsonSerializerSettings() {
// Use this option to ignore reference looping option
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
// Use this option when properties use an Interface as the type
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
};
var serialized = JsonConvert.SerializeObject(source, jsonSettings);
return JsonConvert.DeserializeObject<T>(serialized, jsonSettings);
}
}
Also in C#:
- Title
- repeater itemdatabound event in c#
- Category
- C#
- Title
- .net core convert keycollection to array
- Category
- C#
- Title
- get setter c# model
- Category
- C#
- Title
- how do I attach a player with a navMeshAgent
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- image filter
- Category
- C#
- Title
- get enum by index c#
- Category
- C#
- Title
- uwp file open picker
- Category
- C#
- Title
- unity c# get bool from another script
- Category
- C#
- Title
- c# list all files in a directory and subdirectory
- Category
- C#
- Title
- unity array of child objects
- Category
- C#
- Title
- c# data types
- Category
- C#
- Title
- how to make a string a list of characters c#
- Category
- C#
- Title
- copy a list in c# unity
- Category
- C#
- Title
- chase object unity
- Category
- C#
- Title
- c# length of array
- Category
- C#
- Title
- C# assigning image location
- Category
- C#
- Title
- c# return multiple value unity
- Category
- C#
- Title
- ecs get specific entities with component
- Category
- C#
- Title
- c# round number down
- Category
- C#
- Title
- nunit return parameter
- Category
- C#
- Title
- C# save pdf stream to file
- Category
- C#
- Title
- c# 2 timespan return yesterday
- Category
- C#
- Title
- convert word files to plain text c#
- Category
- C#
- Title
- check if string is email c#
- Category
- C#
- Title
- c# list string return concatenate
- Category
- C#
- Title
- change textbox location C#
- Category
- C#
- Title
- remove all array elements c#
- Category
- C#
- Title
- unity custom update
- Category
- C#
- Title
- c# md5 hash file
- Category
- C#
- Title
- c sharp split by newline
- Category
- C#
- Title
- c# change colour of console
- Category
- C#
- Title
- unity fall damage c#
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- c# read file stream
- Category
- C#
- Title
- how to convert iformfile to byte array c#
- Category
- C#
- Title
- wpf get name of clicked element
- Category
- C#
- Title
- how to delay something in c# unity
- Category
- C#
- Title
- c# shorten an method
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- c# array to list
- Category
- C#
- Title
- how to trigger event when a com device is connected in c#
- Category
- C#
- Title
- unity hide mouse
- Category
- C#
- Title
- c# list.foreach
- Category
- C#
- Title
- c# make request to rest api
- Category
- C#
- Title
- c# keyvaluepair
- Category
- C#
- Title
- c# regex to find number between parenthesis
- Category
- C#
- Title
- constructor in inherited class c#
- Category
- C#
- Title
- c# generate random number
- Category
- C#
- Title
- landscape print gridcontrol devexpress
- Category
- C#
- Title
- unity getcomponent
- Category
- C#
- Title
- how to do a messagebox in c#
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- httpcontext in .net standard
- Category
- C#
- Title
- call a function c#
- Category
- C#
- Title
- c# MD5.Create returning nul
- Category
- C#
- Title
- move towards target unity
- Category
- C#
- Title
- c# read a webpage data
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- create asp.net which send email and sms using own api
- Category
- C#
- Title
- C# random.Next error
- Category
- C#
- Title
- ignore fakeiteasy
- Category
- C#
- Title
- scaffold-dbcontext sql server
- Category
- C#
- Title
- regex in c#
- Category
- C#
- Title
- multidimensional meaning
- Category
- C#
- Title
- c# summary tag
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- move file from one folder to another c#
- Category
- C#
- Title
- unitt only 1 number float
- Category
- C#
- Title
- unity 2d rotate towards direction
- Category
- C#
- Title
- c# get current directory xamarin
- Category
- C#
- Title
- how to join array indexes with comma in c#
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- convert string to boolean c#
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- unity set material
- Category
- C#
- Title
- unity delete specific text in a string
- Category
- C#
- Title
- xarray: create 2d dataset
- Category
- C#
- Title
- convert number of days into months c#
- Category
- C#
- Title
- c# check if string is in array
- Category
- C#
- Title
- IEnumerator
- Category
- C#
- Title
- delegate function c#
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- create char array c#
- Category
- C#
- Title
- .net core get image from url
- Category
- C#
- Title
- how get data from json in c#
- Category
- C#
- Title
- leantween id
- Category
- C#
- Title
- c# inline if
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- unity detect any key
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- unity gameobject.find
- Category
- C#
- Title
- cursorlockmode cannot be used as a method C#
- Category
- C#
- Title
- unity how to change rotation
- Category
- C#
- Title
- c sharp check if key in dictionary
- Category
- C#
- Title
- orderby make sunday last day c#
- Category
- C#
- Title
- unity how to load up a scene
- Category
- C#
- Title
- unity transfoprm position y change
- Category
- C#
- Title
- get set c#
- Category
- C#
- Title
- c# string to memorystream
- Category
- C#