The Labs \ Source Viewer \ SSCLI \ System \ Object

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  Object
  18. **
  19. **
  20. ** Object is the root class for all CLR objects.  This class
  21. ** defines only the basics.
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System
  26. {
  27.     using System;
  28.     using System.Runtime.InteropServices;
  29.     using System.Runtime.CompilerServices;
  30.     using System.Runtime.ConstrainedExecution;
  31.     using CultureInfo = System.Globalization.CultureInfo;
  32.     using FieldInfo = System.Reflection.FieldInfo;
  33.     using BindingFlags = System.Reflection.BindingFlags;
  34.     using RemotingException = System.Runtime.Remoting.RemotingException;
  35.     // The Object is the root class for all object in the CLR System. Object
  36.     // is the super class for all other CLR objects and provide a set of methods and low level
  37.     // services to subclasses. These services include object synchronization and support for clone
  38.     // operations.
  39.     //
  40.     //This class contains no data and does not need to be serializable
  41.     [Serializable()]
  42.     [ClassInterface(ClassInterfaceType.AutoDual)]
  43.     [System.Runtime.InteropServices.ComVisible(true)]
  44.     public class Object
  45.     {
  46.         // Creates a new instance of an Object.
  47.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  48.         public Object()
  49.         {
  50.         }
  51.        
  52.         // Returns a String which represents the object instance. The default
  53.         // for an object is to return the fully qualified name of the class.
  54.         //
  55.         public virtual string ToString()
  56.         {
  57.             return GetType().ToString();
  58.         }
  59.        
  60.         // Returns a boolean indicating if the passed in object obj is
  61.         // Equal to this. Equality is defined as object equality for reference
  62.         // types and bitwise equality for value types using a loader trick to
  63.         // replace Equals with EqualsValue for value types).
  64.         //
  65.         public virtual bool Equals(object obj)
  66.         {
  67.             return InternalEquals(this, obj);
  68.         }
  69.        
  70.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  71.         static internal extern bool InternalEquals(object objA, object objB);
  72.        
  73.         public static bool Equals(object objA, object objB)
  74.         {
  75.             if (objA == objB) {
  76.                 return true;
  77.             }
  78.             if (objA == null || objB == null) {
  79.                 return false;
  80.             }
  81.             return objA.Equals(objB);
  82.         }
  83.        
  84.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  85.         public static bool ReferenceEquals(object objA, object objB)
  86.         {
  87.             return objA == objB;
  88.         }
  89.        
  90.         // GetHashCode is intended to serve as a hash function for this object.
  91.         // Based on the contents of the object, the hash function will return a suitable
  92.         // value with a relatively random distribution over the various inputs.
  93.         //
  94.         // The default implementation returns the sync block index for this instance.
  95.         // Calling it on the same object multiple times will return the same value, so
  96.         // it will technically meet the needs of a hash function, but it's less than ideal.
  97.         // Objects (& especially value classes) should override this method.
  98.         //
  99.         public virtual int GetHashCode()
  100.         {
  101.             return InternalGetHashCode(this);
  102.         }
  103.        
  104.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  105.         static internal extern int InternalGetHashCode(object obj);
  106.        
  107.         // Returns a Type object which represent this object instance.
  108.         //
  109.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  110.         public extern Type GetType();
  111.        
  112.         // Allow an object to free resources before the object is reclaimed by the GC.
  113.         //
  114.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  115.         ~Object()
  116.         {
  117.         }
  118.        
  119.         // Returns a new object instance that is a memberwise copy of this
  120.         // object. This is always a shallow copy of the instance. The method is protected
  121.         // so that other object may only call this method on themselves. It is entended to
  122.         // support the ICloneable interface.
  123.         //
  124.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  125.         protected extern object MemberwiseClone();
  126.        
  127.        
  128.         // Sets the value specified in the variant on the field
  129.         //
  130.         private void FieldSetter(string typeName, string fieldName, object val)
  131.         {
  132.             // Extract the field info object
  133.             FieldInfo fldInfo = GetFieldInfo(typeName, fieldName);
  134.            
  135.             if (fldInfo.IsInitOnly)
  136.                 throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly"));
  137.            
  138.             // Make sure that the value is compatible with the type
  139.             // of field
  140.             System.Runtime.Remoting.Messaging.Message.CoerceArg(val, fldInfo.FieldType);
  141.            
  142.             // Set the value
  143.             fldInfo.SetValue(this, val);
  144.         }
  145.        
  146.         // Gets the value specified in the variant on the field
  147.         //
  148.         private void FieldGetter(string typeName, string fieldName, ref object val)
  149.         {
  150.             // Extract the field info object
  151.             FieldInfo fldInfo = GetFieldInfo(typeName, fieldName);
  152.            
  153.             // Get the value
  154.             val = fldInfo.GetValue(this);
  155.         }
  156.        
  157.         // Gets the field info object given the type name and field name.
  158.         //
  159.         private FieldInfo GetFieldInfo(string typeName, string fieldName)
  160.         {
  161.             Type t = GetType();
  162.             while (null != t) {
  163.                 if (t.FullName.Equals(typeName)) {
  164.                     break;
  165.                 }
  166.                
  167.                 t = t.BaseType;
  168.             }
  169.            
  170.             if (null == t) {
  171.                 throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), typeName));
  172.             }
  173.            
  174.             FieldInfo fldInfo = t.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  175.             if (null == fldInfo) {
  176.                 throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadField"), fieldName, typeName));
  177.             }
  178.            
  179.             return fldInfo;
  180.         }
  181.     }
  182.    
  183.    
  184.     // Internal methodtable used to instantiate the "canonical" methodtable for generic instantiations.
  185.     // The name "__Canon" will never been seen by users but it will appear a lot in debugger stack traces
  186.     // involving generics so it is kept deliberately short as to avoid being a nuisance.
  187.    
  188.     [Serializable()]
  189.     [ClassInterface(ClassInterfaceType.AutoDual)]
  190.     [System.Runtime.InteropServices.ComVisible(true)]
  191.     internal class __Canon
  192.     {
  193.     }
  194.    
  195. }

Developer Fusion