| | | 1 | | using Newtonsoft.Json; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides generic helper extensions, including deep-cloning via JSON serialization. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class GenericExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a deep clone of the provided object via JSON serialization. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="T">Type of the object.</typeparam> |
| | | 14 | | /// <param name="source">The instance to clone.</param> |
| | | 15 | | /// <returns>A cloned instance of T, or null if serialization fails.</returns> |
| | | 16 | | public static T? Clone<T>(this T source) |
| | 3 | 17 | | { |
| | 3 | 18 | | var serialized = JsonConvert.SerializeObject(source); |
| | | 19 | | |
| | 3 | 20 | | return JsonConvert.DeserializeObject<T>(serialized); |
| | 3 | 21 | | } |
| | | 22 | | } |