25 lines
796 B
C#
25 lines
796 B
C#
namespace System
|
|
{
|
|
using Newtonsoft.Json;
|
|
|
|
public static class CloneObjectExtension
|
|
{
|
|
/// <summary>
|
|
/// Caution: we should check performance for using it for cloning a collection of small objects such as scenario details or allocations
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static T Clone<T>(this T source)
|
|
{
|
|
if (Object.ReferenceEquals(source, null))
|
|
return default(T);
|
|
|
|
var jsonObject = JsonConvert.SerializeObject(source);
|
|
if (string.IsNullOrWhiteSpace(jsonObject))
|
|
return default(T);
|
|
|
|
return JsonConvert.DeserializeObject<T>(jsonObject);
|
|
}
|
|
}
|
|
} |