Style cleanup
This commit is contained in:
@@ -25,11 +25,9 @@ public static class DeepCloneHandler
|
||||
/// Recursive references will be handled correctly, and will only be cloned once, to prevent infinite loops and invalid
|
||||
/// references.
|
||||
/// </summary>
|
||||
public static T DeepClone<T>(this T? obj, Dictionary<(Type, int), object>? objects = null) where T : IDeepCloneable
|
||||
{
|
||||
return (T)DeepClone((object?)obj, objects)!;
|
||||
}
|
||||
|
||||
public static T DeepClone<T>(this T? obj, Dictionary<(Type, int), object>? objects = null)
|
||||
where T : IDeepCloneable => (T)DeepClone((object?)obj, objects)!;
|
||||
|
||||
private static object? DeepClone(this object? obj, Dictionary<(Type, int), object>? objects = null)
|
||||
{
|
||||
if (obj == null)
|
||||
@@ -41,7 +39,7 @@ public static class DeepCloneHandler
|
||||
// We use GetUninitializedObject to create an object without calling the constructor. This is necessary to prevent
|
||||
// side effects from the constructor, and to not require a parameterless constructor.
|
||||
var newObj = FormatterServices.GetUninitializedObject(type)!;
|
||||
|
||||
|
||||
// If the objects dictionary is null, we create a new one. We use this dictionary to keep track of objects that have
|
||||
// already been cloned, so we can re-use them instead of cloning them again. This is necessary to prevent infinite
|
||||
// loops and invalid references.
|
||||
@@ -74,7 +72,7 @@ public static class DeepCloneHandler
|
||||
// If the object is a value type or a string, we can just return it.
|
||||
if (type.IsValueType || type == typeof(string))
|
||||
return obj;
|
||||
|
||||
|
||||
// If the object is marked as deep cloneable, we will clone it.
|
||||
if (type.GetInterface(nameof(IDeepCloneable)) != null || ExternalDeepCloneTypes.Contains(type))
|
||||
{
|
||||
@@ -91,8 +89,9 @@ public static class DeepCloneHandler
|
||||
var array = (Array)obj;
|
||||
var newArray = Array.CreateInstance(type.GetElementType()!, array.Length);
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
newArray.SetValue(DeepCloneInternal(array.GetValue(i), type.GetElementType()!, objects),
|
||||
i);
|
||||
{
|
||||
newArray.SetValue(DeepCloneInternal(array.GetValue(i), type.GetElementType()!, objects), i);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
@@ -115,9 +114,10 @@ public static class DeepCloneHandler
|
||||
var dictionary = (IDictionary)obj;
|
||||
var newDictionary = (IDictionary)Activator.CreateInstance(type);
|
||||
foreach (DictionaryEntry entry in dictionary)
|
||||
newDictionary.Add(
|
||||
DeepCloneInternal(entry.Key, type.GetGenericArguments()[0], objects)!,
|
||||
{
|
||||
newDictionary.Add(DeepCloneInternal(entry.Key, type.GetGenericArguments()[0], objects)!,
|
||||
DeepCloneInternal(entry.Value, type.GetGenericArguments()[1], objects));
|
||||
}
|
||||
return newDictionary;
|
||||
}
|
||||
}
|
||||
@@ -133,8 +133,7 @@ public static class DeepCloneHandler
|
||||
/// This method is thread safe, and will only create the expressions once for each type. It returns compiled expressions for
|
||||
/// each field in the type, so that we can get high performance deep cloning.
|
||||
/// </remarks>
|
||||
private static (Func<object, object?> getter, Action<object, object?> setter)[]
|
||||
GetDeepCloneExpressions(Type type)
|
||||
private static (Func<object, object?> getter, Action<object, object?> setter)[] GetDeepCloneExpressions(Type type)
|
||||
{
|
||||
// We use a lock here to prevent multiple threads from trying to create the expressions at the same time.
|
||||
lock (DeepCloneExpressions)
|
||||
|
||||
Reference in New Issue
Block a user