MarshalByRefObject.MemberwiseClone(Boolean) Method

Definition

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

protected:
 MarshalByRefObject ^ MemberwiseClone(bool cloneIdentity);
protected MarshalByRefObject MemberwiseClone (bool cloneIdentity);
override this.MemberwiseClone : bool -> MarshalByRefObject
Protected Function MemberwiseClone (cloneIdentity As Boolean) As MarshalByRefObject

Parameters

cloneIdentity
Boolean

如果要删除当前 MarshalByRefObject 对象的标识,则为 false,这使该对象在跨远程边界封送时分配一个新标识。false to delete the current MarshalByRefObject object's identity, which will cause the object to be assigned a new identity when it is marshaled across a remoting boundary. false 通常比较合适。A value of false is usually appropriate. 如果为 true,则将当前 MarshalByRefObject 对象的标识复制到它的克隆项,这会将远程客户端调用路由到远程服务器对象。true to copy the current MarshalByRefObject object's identity to its clone, which will cause remoting client calls to be routed to the remote server object.

Returns

MarshalByRefObject

当前 MarshalByRefObject 对象的浅表副本。A shallow copy of the current MarshalByRefObject object.

Remarks

MemberwiseClone(Boolean) 方法创建一个浅表副本,方法是创建一个新的 MarshalByRefObject 对象,然后将当前 MarshalByRefObject 对象的非静态字段复制到新的对象。The MemberwiseClone(Boolean) method creates a shallow copy by creating a new MarshalByRefObject object, and then copying the nonstatic fields of the current MarshalByRefObject object to the new object. 如果字段是值类型,则执行字段的逐位副本。If a field is a value type, a bit-by-bit copy of the field is performed. 如果字段是引用类型,则会复制引用,但不会复制引用的对象;因此,原始对象及其复本引用相同的对象。If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

例如,假设有一个名为 X 的 MarshalByRefObject 对象,该对象引用对象 A 和 B。对象 B 反过来引用对象 C。X 的浅表副本创建新的对象 X2,后者同时引用对象 A 和 B。与之相比,X 的深层副本会创建一个新的对象 X2,该对象引用了新的对象 A2 和 B2 (即 A 和 B 的副本),进而引用新的对象 C2,这是一个副本 C。使用实现 ICloneable 接口的类来执行对象的深层或浅表复制。For example, consider a MarshalByRefObject object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy C. Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.

MarshalByRefObject 对象的标识定义为作为远程处理客户端调用目标的远程服务器对象。The identity of a MarshalByRefObject object is defined as the remote server object that is the target of a remoting client call. 默认情况下,MarshalByRefObject 对象的按成员克隆具有与原始对象相同的标识,对于跨远程处理边界封送到客户端的服务器端对象的克隆,这通常不是正确的行为。By default, the memberwise clone of a MarshalByRefObject object has the same identity as the original object, which is typically not the correct behavior for clones of server-side objects that are marshaled across a remoting boundary to the client side. 指定 false,这通常适用于删除克隆的标识并导致在跨远程处理边界封送克隆时分配新标识,或 true 使克隆保留原始 MarshalByRefObject 对象的标识。Specify false, which is usually appropriate, to delete the identity of the clone and cause a new identity to be assigned when the clone is marshaled across a remoting boundary, or true to cause the clone to retain the identity of the original MarshalByRefObject object. MemberwiseClone(Boolean) 方法旨在供实现远程服务器对象的开发人员使用。The MemberwiseClone(Boolean) method is intended to be used by developers implementing remote server objects.

Applies to