ObjectExtensionsDeepCloneT(T, Boolean) Method

Note: This API is now obsolete.
Clones an object by deep cloning.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.2.0
C#
[ObsoleteAttribute("This DeepClone overload is obsolete. Use the DeepClone<T>(T,Func<object,object?>?) overload instead.")]
public static T DeepClone<T>(
	this T obj,
	bool ignoreCustomSerialization = false
)

Parameters

obj  T
The object to clone.
ignoreCustomSerialization  Boolean  (Optional)
to ignore ISerializable and IObjectReference implementations as well as serialization constructors and serializing methods; to consider all of these techniques instead of performing a forced field-based serialization. This parameter is optional.
Default value: .

Type Parameters

T
Type of the object

Return Value

T
The functionally equivalent clone of the object.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type T. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This method makes possible to clone objects even if their type is not marked by the SerializableAttribute; however, in such case it is not guaranteed that the result is functionally equivalent to the input object.

  Caution

In .NET Core there are some types that implement the ISerializable interface, though they are not serializable. In such cases the cloning attempt typically throws a PlatformNotSupportedException. To clone such objects the ignoreCustomSerialization parameter should be .

If ignoreCustomSerialization is , then it is not guaranteed that the object can be cloned in all circumstances (see the note above).

On the other hand, if ignoreCustomSerialization is , then it can happen that even singleton types will be deep cloned. The cloning is performed by the BinarySerializationFormatter class, which supports some singleton types natively (such as Type and DBNull), which will be always cloned correctly.

In .NET Framework remote objects are cloned in a special way and the result is always a local object. The ignoreCustomSerialization parameter is ignored for remote objects.

See Also