diff options
Diffstat (limited to 'src/mscorlib/src/System/Object.cs')
-rw-r--r-- | src/mscorlib/src/System/Object.cs | 320 |
1 files changed, 161 insertions, 159 deletions
diff --git a/src/mscorlib/src/System/Object.cs b/src/mscorlib/src/System/Object.cs index 1f47d8cdef..643e1d933e 100644 --- a/src/mscorlib/src/System/Object.cs +++ b/src/mscorlib/src/System/Object.cs @@ -25,200 +25,202 @@ namespace System using FieldInfo = System.Reflection.FieldInfo; using BindingFlags = System.Reflection.BindingFlags; -// The Object is the root class for all object in the CLR System. Object -// is the super class for all other CLR objects and provide a set of methods and low level -// services to subclasses. These services include object synchronization and support for clone -// operations. -// - //This class contains no data and does not need to be serializable -[Serializable] -[ClassInterface(ClassInterfaceType.AutoDual)] -[System.Runtime.InteropServices.ComVisible(true)] -public class Object -{ - // Creates a new instance of an Object. - [System.Runtime.Versioning.NonVersionable] - public Object() - { - } - - // Returns a String which represents the object instance. The default - // for an object is to return the fully qualified name of the class. - // - public virtual String ToString() - { - return GetType().ToString(); - } - - // Returns a boolean indicating if the passed in object obj is - // Equal to this. Equality is defined as object equality for reference - // types and bitwise equality for value types using a loader trick to - // replace Equals with EqualsValue for value types). + // The Object is the root class for all object in the CLR System. Object + // is the super class for all other CLR objects and provide a set of methods and low level + // services to subclasses. These services include object synchronization and support for clone + // operations. // - - public virtual bool Equals(Object obj) + //This class contains no data and does not need to be serializable + [Serializable] + [ClassInterface(ClassInterfaceType.AutoDual)] + [System.Runtime.InteropServices.ComVisible(true)] + public class Object { - return RuntimeHelpers.Equals(this, obj); - } - - public static bool Equals(Object objA, Object objB) - { - if (objA==objB) { - return true; + // Creates a new instance of an Object. + [System.Runtime.Versioning.NonVersionable] + public Object() + { } - if (objA==null || objB==null) { - return false; + + // Returns a String which represents the object instance. The default + // for an object is to return the fully qualified name of the class. + // + public virtual String ToString() + { + return GetType().ToString(); } - return objA.Equals(objB); - } - [System.Runtime.Versioning.NonVersionable] - public static bool ReferenceEquals (Object objA, Object objB) { - return objA == objB; - } - - // GetHashCode is intended to serve as a hash function for this object. - // Based on the contents of the object, the hash function will return a suitable - // value with a relatively random distribution over the various inputs. - // - // The default implementation returns the sync block index for this instance. - // Calling it on the same object multiple times will return the same value, so - // it will technically meet the needs of a hash function, but it's less than ideal. - // Objects (& especially value classes) should override this method. - // - public virtual int GetHashCode() - { - return RuntimeHelpers.GetHashCode(this); - } - - // Returns a Type object which represent this object instance. - // - [Pure] - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public extern Type GetType(); + // Returns a boolean indicating if the passed in object obj is + // Equal to this. Equality is defined as object equality for reference + // types and bitwise equality for value types using a loader trick to + // replace Equals with EqualsValue for value types). + // - // Allow an object to free resources before the object is reclaimed by the GC. - // - [System.Runtime.Versioning.NonVersionable] - ~Object() - { - } - - // Returns a new object instance that is a memberwise copy of this - // object. This is always a shallow copy of the instance. The method is protected - // so that other object may only call this method on themselves. It is entended to - // support the ICloneable interface. - // - [MethodImplAttribute(MethodImplOptions.InternalCall)] - protected extern Object MemberwiseClone(); - - - // Sets the value specified in the variant on the field - // - private void FieldSetter(String typeName, String fieldName, Object val) - { - Contract.Requires(typeName != null); - Contract.Requires(fieldName != null); + public virtual bool Equals(Object obj) + { + return RuntimeHelpers.Equals(this, obj); + } - // Extract the field info object - FieldInfo fldInfo = GetFieldInfo(typeName, fieldName); + public static bool Equals(Object objA, Object objB) + { + if (objA == objB) + { + return true; + } + if (objA == null || objB == null) + { + return false; + } + return objA.Equals(objB); + } - if (fldInfo.IsInitOnly) - throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly")); + [System.Runtime.Versioning.NonVersionable] + public static bool ReferenceEquals(Object objA, Object objB) + { + return objA == objB; + } - // Make sure that the value is compatible with the type - // of field - Type pt=fldInfo.FieldType; - if (pt.IsByRef) + // GetHashCode is intended to serve as a hash function for this object. + // Based on the contents of the object, the hash function will return a suitable + // value with a relatively random distribution over the various inputs. + // + // The default implementation returns the sync block index for this instance. + // Calling it on the same object multiple times will return the same value, so + // it will technically meet the needs of a hash function, but it's less than ideal. + // Objects (& especially value classes) should override this method. + // + public virtual int GetHashCode() { - pt = pt.GetElementType(); + return RuntimeHelpers.GetHashCode(this); } - if (!pt.IsInstanceOfType(val)) + // Returns a Type object which represent this object instance. + // + [Pure] + [MethodImplAttribute(MethodImplOptions.InternalCall)] + public extern Type GetType(); + + // Allow an object to free resources before the object is reclaimed by the GC. + // + [System.Runtime.Versioning.NonVersionable] + ~Object() { - val = Convert.ChangeType(val, pt, CultureInfo.InvariantCulture); } - // Set the value - fldInfo.SetValue(this, val); - } + // Returns a new object instance that is a memberwise copy of this + // object. This is always a shallow copy of the instance. The method is protected + // so that other object may only call this method on themselves. It is entended to + // support the ICloneable interface. + // + [MethodImplAttribute(MethodImplOptions.InternalCall)] + protected extern Object MemberwiseClone(); - // Gets the value specified in the variant on the field - // - private void FieldGetter(String typeName, String fieldName, ref Object val) - { - Contract.Requires(typeName != null); - Contract.Requires(fieldName != null); - // Extract the field info object - FieldInfo fldInfo = GetFieldInfo(typeName, fieldName); + // Sets the value specified in the variant on the field + // + private void FieldSetter(String typeName, String fieldName, Object val) + { + Contract.Requires(typeName != null); + Contract.Requires(fieldName != null); - // Get the value - val = fldInfo.GetValue(this); - } + // Extract the field info object + FieldInfo fldInfo = GetFieldInfo(typeName, fieldName); - // Gets the field info object given the type name and field name. - // - private FieldInfo GetFieldInfo(String typeName, String fieldName) - { - Contract.Requires(typeName != null); - Contract.Requires(fieldName != null); - Contract.Ensures(Contract.Result<FieldInfo>() != null); + if (fldInfo.IsInitOnly) + throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly")); - Type t = GetType(); - while(null != t) - { - if(t.FullName.Equals(typeName)) + // Make sure that the value is compatible with the type + // of field + Type pt = fldInfo.FieldType; + if (pt.IsByRef) { - break; + pt = pt.GetElementType(); } - t = t.BaseType; - } - - if (null == t) - { - throw new ArgumentException(); + if (!pt.IsInstanceOfType(val)) + { + val = Convert.ChangeType(val, pt, CultureInfo.InvariantCulture); + } + + // Set the value + fldInfo.SetValue(this, val); } - FieldInfo fldInfo = t.GetField(fieldName, BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.IgnoreCase); - if(null == fldInfo) + // Gets the value specified in the variant on the field + // + private void FieldGetter(String typeName, String fieldName, ref Object val) { - throw new ArgumentException(); + Contract.Requires(typeName != null); + Contract.Requires(fieldName != null); + + // Extract the field info object + FieldInfo fldInfo = GetFieldInfo(typeName, fieldName); + + // Get the value + val = fldInfo.GetValue(this); } - return fldInfo; - } -} + // Gets the field info object given the type name and field name. + // + private FieldInfo GetFieldInfo(String typeName, String fieldName) + { + Contract.Requires(typeName != null); + Contract.Requires(fieldName != null); + Contract.Ensures(Contract.Result<FieldInfo>() != null); + Type t = GetType(); + while (null != t) + { + if (t.FullName.Equals(typeName)) + { + break; + } -// Internal methodtable used to instantiate the "canonical" methodtable for generic instantiations. -// The name "__Canon" will never been seen by users but it will appear a lot in debugger stack traces -// involving generics so it is kept deliberately short as to avoid being a nuisance. + t = t.BaseType; + } -[Serializable] -[ClassInterface(ClassInterfaceType.AutoDual)] -[System.Runtime.InteropServices.ComVisible(true)] -internal class __Canon -{ -} + if (null == t) + { + throw new ArgumentException(); + } -// This class is used to define the name of the base class library -internal class CoreLib -{ - public const string Name = "System.Private.CoreLib"; + FieldInfo fldInfo = t.GetField(fieldName, BindingFlags.Public | + BindingFlags.Instance | + BindingFlags.IgnoreCase); + if (null == fldInfo) + { + throw new ArgumentException(); + } - public static string FixupCoreLibName(string strToFixup) - { - if (!String.IsNullOrEmpty(strToFixup)) - { - strToFixup = strToFixup.Replace("mscorlib", System.CoreLib.Name); + return fldInfo; } + } + - return strToFixup; + // Internal methodtable used to instantiate the "canonical" methodtable for generic instantiations. + // The name "__Canon" will never been seen by users but it will appear a lot in debugger stack traces + // involving generics so it is kept deliberately short as to avoid being a nuisance. + + [Serializable] + [ClassInterface(ClassInterfaceType.AutoDual)] + [System.Runtime.InteropServices.ComVisible(true)] + internal class __Canon + { } -} + // This class is used to define the name of the base class library + internal class CoreLib + { + public const string Name = "System.Private.CoreLib"; + + public static string FixupCoreLibName(string strToFixup) + { + if (!String.IsNullOrEmpty(strToFixup)) + { + strToFixup = strToFixup.Replace("mscorlib", System.CoreLib.Name); + } + + return strToFixup; + } + } } |