ICloneable 介面

定義

支援複製 (Clone),使用與現有執行個體 (Instance) 相同的值建立新的執行個體。Supports cloning, which creates a new instance of a class with the same value as an existing instance.

public interface class ICloneable
public interface ICloneable
[System.Runtime.InteropServices.ComVisible(true)]
public interface ICloneable
type ICloneable = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type ICloneable = interface
Public Interface ICloneable
衍生
屬性

備註

ICloneable介面可讓您提供自訂的執行,以建立現有物件的複本。The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object. ICloneable介面包含一個成員( Clone 方法),其目的是要提供超過所提供的複製支援 Object.MemberwiseCloneThe ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone. 如需有關複製、深層和淺層複製以及範例的詳細資訊,請參閱 Object.MemberwiseClone 方法。For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method.

給實施者的注意事項

ICloneable介面只會要求您的方法的執行會傳回 Clone() 目前物件實例的複本。The ICloneable interface simply requires that your implementation of the Clone() method return a copy of the current object instance. 它不會指定複製作業會執行深層複製、淺層複製,還是介於兩者之間的事物。It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. 也不需要將原始實例的所有屬性值複製到新的實例。Nor does it require all property values of the original instance to be copied to the new instance. 例如, Clone() 方法會執行所有屬性的淺層複製(屬性除外), IsReadOnly 它一律會將這個屬性值設定為 false 複製之物件中的。For example, the Clone() method performs a shallow copy of all properties except the IsReadOnly property; it always sets this property value to false in the cloned object. 因為的呼叫端 Clone() 無法依賴執行可預測複製作業的方法,所以我們建議 ICloneable 不要在公用 api 中執行。Because callers of Clone() cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.

方法

Clone()

建立新的物件,這個物件是目前執行個體的複本。Creates a new object that is a copy of the current instance.

適用於

另請參閱