internal static void SwitchTo(bool isSafe) { DeepClonerCache.ClearCache(); if (isSafe) { _instance = new ShallowSafeObjectCloner(); } else { _instance = _unsafeInstance; } }
private static object GenerateCloner(Type t, bool asObject) { if (DeepClonerSafeTypes.CanReturnSameObject(t) && asObject && !t.IsValueType()) { return(null); } if (ShallowObjectCloner.IsSafeVariant()) { return(DeepClonerExprGenerator.GenerateClonerInternal(t, asObject)); } return(DeepClonerMsilGenerator.GenerateClonerInternal(t, asObject)); }
static ShallowObjectCloner() { _unsafeInstance = GenerateUnsafeCloner(); _instance = _unsafeInstance; try { _instance.DoCloneObject(new object()); } catch (Exception) { _instance = new ShallowSafeObjectCloner(); } }
public static T CloneObject <T>(T obj) { if (obj is ValueType) { if (typeof(T) == obj.GetType()) { return(obj); } return((T)ShallowObjectCloner.CloneObject(obj)); } if (obj == null) { return((T)(object)null); } if (DeepClonerSafeTypes.CanReturnSameObject(obj.GetType())) { return(obj); } return((T)ShallowObjectCloner.CloneObject(obj)); }