首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

object 类中的函数:protected object MemberwiseClone(),作用是什么呀,小弟我找不到实现的代码呀

2013-01-23 
object 类中的函数:protected object MemberwiseClone(),作用是什么呀,我找不到实现的代码呀?object 类中

object 类中的函数:protected object MemberwiseClone(),作用是什么呀,我找不到实现的代码呀?
object 类中的函数:protected object MemberwiseClone(),作用是什么呀,我找不到实现的代码呀?
以下是vs2008中自带的源码,object 中有 MemberwiseClone() 方法,但是我不知道功能是什么,为何没有实现的代码呢?
如何能看到实现的代码呢?


using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

namespace System
{
    // 摘要:
    //     Supports all classes in the .NET Framework class hierarchy and provides low-level
    //     services to derived classes. This is the ultimate base class of all classes
    //     in the .NET Framework; it is the root of the type hierarchy.
    [Serializable]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Object
    {
        // 摘要:
        //     Initializes a new instance of the System.Object class.
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        public Object();

        // 摘要:
        //     Determines whether the specified System.Object is equal to the current System.Object.
        //
        // 参数:
        //   obj:
        //     The System.Object to compare with the current System.Object.
        //
        // 返回结果:
        //     true if the specified System.Object is equal to the current System.Object;
        //     otherwise, false.
        //
        // 异常:
        //   System.NullReferenceException:
        //     The obj parameter is null.
        public virtual bool Equals(object obj);
        //
        // 摘要:
        //     Determines whether the specified System.Object instances are considered equal.
        //
        // 参数:
        //   objA:
        //     The first System.Object to compare.
        //


        //   objB:
        //     The second System.Object to compare.
        public static bool Equals(object objA, object objB);
        //
        // 摘要:
        //     Serves as a hash function for a particular type.
        //
        // 返回结果:
        //     A hash code for the current System.Object.
        public virtual int GetHashCode();
        //
        // 摘要:
        //     Gets the System.Type of the current instance.
        //
        // 返回结果:
        //     The System.Type instance that represents the exact runtime type of the current
        //     instance.
        public Type GetType();
        //
        // 摘要:
        //     Creates a shallow copy of the current System.Object.
        //
        // 返回结果:
        //     A shallow copy of the current System.Object.
        protected object MemberwiseClone();
        //
        // 摘要:
        //     Determines whether the specified System.Object instances are the same instance.
        //
        // 参数:
        //   objA:
        //     The first System.Object to compare.
        //
        //   objB:
        //     The second System.Object to compare.
        //
        // 返回结果:
        //     true if objA is the same instance as objB or if both are null references;
        //     otherwise, false.
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        public static bool ReferenceEquals(object objA, object objB);


        //
        // 摘要:
        //     Returns a System.String that represents the current System.Object.
        //
        // 返回结果:
        //     A System.String that represents the current System.Object.
        public virtual string ToString();
    }
}


[解决办法]
用ILasm 反编译,或者用Reflector
[解决办法]
浅拷贝使用Object类MemberwiseClone实现  
MemberwiseClone:创建当前 Object 的浅表副本
浅拷贝(shallow copy)对于引用类型对象中的值类型字段进行了逐位复制。赋值运算符只是把源对象的引用赋值给目的对象,两者引用同一个对象。
浅拷贝后的对象的值类型字段更改不会反映到源对象,而赋值运算后的对象的值类型字段更改会反映到源对象 using System;

class DeepCopy : ICloneable
{
  public int i;
  public DeepCopy()
  {
  }
  // 供Clone方法调用的私有构造函数
  private DeepCopy(int i)
  {
  this.v = (int)i.Clone();
  }
  public Object Clone()
  {
  // 构造一个新的DeepCopy对象
  return new DeepCopy(this.i);
  }
  public void Display()
  {
  Console.Write( i + ", ");
  Console.WriteLine();
  }
}

热点排行