.Net System.Object 对象实现代码

  1// Decompiled by Salamander version 1.0.6
  2// Copyright 2002 Remotesoft Inc. All rights reserved.
  3// http://www.remotesoft.com/salamander
  4
  5using System.Reflection;
  6using System.Runtime;
  7using System.Runtime.Remoting;
  8using System.Runtime.Remoting.Messaging;
  9
 10namespace System
 11{
 12  [ClassInterfaceAttribute(ClassInterfaceType.AutoDual)]
 13  [SerializableAttribute()]
 14  public class Object
 15  {
 16
 17    public Object()
 18    {
 19    }

 20
 21    // internalcall
 22    private Type InternalGetType();
 23
 24    // internalcall
 25    private Type FastGetExistingType();
 26
 27    public virtual string ToString()
 28    {
 29      return GetType().FullName;
 30    }

 31
 32    // internalcall
 33    public virtual bool Equals(object obj);
 34
 35    public static bool Equals(object objA, object objB)
 36    {
 37      if (objA == objB)
 38      {
 39        return true;
 40      }

 41      if (objA == null || objB == null)
 42      {
 43        return false;
 44      }

 45      else
 46      {
 47        return objA.Equals(objB);
 48      }

 49    }

 50
 51    public static bool ReferenceEquals(object objA, object objB)
 52    {
 53      return objA == objB;
 54    }

 55
 56    // internalcall
 57    public virtual int GetHashCode();
 58
 59    public Type GetType()
 60    {
 61      Type type = FastGetExistingType();
 62      if (type == null)
 63      {
 64        type = InternalGetType();
 65      }

 66      return type;
 67    }

 68
 69    ~Object()
 70    {
 71    }

 72
 73    // internalcall
 74    protected object MemberwiseClone();
 75
 76    private void FieldSetter(string typeName, string fieldName, object val)
 77    {
 78      FieldInfo fieldInfo = GetFieldInfo(typeName, fieldName);
 79      if (fieldInfo.IsInitOnly)
 80      {
 81        throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly"));
 82      }

 83      Message.CoerceArg(val, fieldInfo.FieldType);
 84      fieldInfo.SetValue(this, val);
 85    }

 86
 87    private void FieldGetter(string typeName, string fieldName, ref object val)
 88    {
 89      FieldInfo fieldInfo = GetFieldInfo(typeName, fieldName);
 90      val = fieldInfo.GetValue(this);
 91    }

 92
 93    private FieldInfo GetFieldInfo(string typeName, string fieldName)
 94    {
 95      Type type;
 96
 97      for (type = GetType(); type != null && !type.FullName.Equals(typeName); type = type.BaseType)
 98      {
 99      }

100      if (type == null)
101      {
102        throw new RemotingException(String.Format(Environment.GetResourceString("Remoting_BadType"), typeName));
103      }

104      FieldInfo fieldInfo = type.GetField(fieldName, 21);
105      if (fieldInfo == null)
106      {
107        throw new RemotingException(String.Format(Environment.GetResourceString("Remoting_BadField"), fieldName, typeName));
108      }

109      else
110      {
111        return fieldInfo;
112      }

113    }

114  }

115
116}

117
原文地址:https://www.cnblogs.com/jssy/p/329681.html