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
[System.Runtime.InteropServices.ComVisible(true)]
public interface ICloneable
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. 介面包含一個成員, 此方法Clone的目的是要提供超過所提供的複製Object.MemberwiseClone支援。 ICloneableThe 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.

給實施者的注意事項

介面只需要您的Clone()方法的執行, 就能傳回目前物件實例的複本。 ICloneableThe 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.

適用於

另請參閱