Type.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Type.cs / 1 / Type.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
////////////////////////////////////////////////////////////////////////////////
 
namespace System { 

    using System; 
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Reflection.Cache;
    using System.Threading; 
    using System.Runtime.Remoting;
    using System.Runtime.InteropServices; 
    using System.Runtime.CompilerServices; 
    using System.Security.Permissions;
    using System.Collections.Generic; 
    using CultureInfo = System.Globalization.CultureInfo;
    using SignatureHelper = System.Reflection.Emit.SignatureHelper;
    using StackCrawlMark = System.Threading.StackCrawlMark;
    using DebuggerStepThroughAttribute = System.Diagnostics.DebuggerStepThroughAttribute; 

    [Serializable()] 
    [ClassInterface(ClassInterfaceType.None)] 
    [ComDefaultInterface(typeof(_Type))]
[System.Runtime.InteropServices.ComVisible(true)] 
    public abstract class Type : MemberInfo, _Type, IReflect
    {
        public static readonly MemberFilter FilterAttribute;
        public static readonly MemberFilter FilterName; 
        public static readonly MemberFilter FilterNameIgnoreCase;
 
        public static readonly Object Missing = System.Reflection.Missing.Value; 

        public static readonly char Delimiter = '.'; 

        // EmptyTypes is used to indicate that we are looking for someting without any parameters.
        public readonly static Type[] EmptyTypes = new Type[0];
 
        // The Default binder.  We create a single one and expose that.
        private static object defaultBinder; 
 
        // Because the current compiler doesn't support static delegates
        //  the _Filters object is an object that we create to contain all of 
        //  the filters.
        //private static final Type _filterClass = new RuntimeType();
        static Type() {
            __Filters _filterClass = new __Filters(); 
            FilterAttribute = new MemberFilter(_filterClass.FilterAttribute);
            FilterName = new MemberFilter(_filterClass.FilterName); 
            FilterNameIgnoreCase = new MemberFilter(_filterClass.FilterIgnoreCase); 
        }
 
        // Prevent from begin created, and allow subclass
        //      to create.
        protected Type() {}
 

        // MemberInfo Methods.... 
        // The Member type Field. 
        public override MemberTypes MemberType {
                get {return System.Reflection.MemberTypes.TypeInfo;} 
        }

        // Return the class that declared this Field.
        public override Type DeclaringType { 
                get {return this;}
        } 
 
        public virtual MethodBase DeclaringMethod { get { return null; } }
 
        // Return the class that was used to obtain this field.
        public override Type ReflectedType
        {
                get {return this;} 
        }
 
        //////////////////////////////////////////////////////////////////////////////// 
        // This is a static method that returns a Class based upon the name of the class
        // (this name needs to be fully qualified with the package name and is 
        // case-sensitive by default).
        ////

        // this method is required so Object.GetType is not made virtual by the compiler 
        //
        public new Type GetType() 
        { 
            return base.GetType();
        } 

        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Type GetType(String typeName, bool throwOnError, bool ignoreCase) {
            unsafe 
            {
                StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; 
                return RuntimeType.PrivateGetType(typeName, throwOnError, ignoreCase, ref stackMark); 
            }
        } 

        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Type GetType(String typeName, bool throwOnError) {
            unsafe 
            {
                StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; 
                return RuntimeType.PrivateGetType(typeName, throwOnError, false, ref stackMark); 
            }
        } 

        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Type GetType(String typeName) {
            unsafe 
            {
                StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; 
                return RuntimeType.PrivateGetType(typeName, false, false, ref stackMark); 
            }
        } 

        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public static Type ReflectionOnlyGetType(String typeName, bool throwIfNotFound, bool ignoreCase)
        { 
            unsafe
            { 
                StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; 
                return RuntimeType.PrivateGetType(typeName, throwIfNotFound, ignoreCase, true /*reflectionOnly*/, ref stackMark);
            } 
        }

        public virtual Type MakePointerType() { throw new NotSupportedException(); }
        public virtual StructLayoutAttribute StructLayoutAttribute { get { throw new NotSupportedException(); } } 
        public virtual Type MakeByRefType() { throw new NotSupportedException(); }
        public virtual Type MakeArrayType() { throw new NotSupportedException(); } 
        public virtual Type MakeArrayType(int rank) { throw new NotSupportedException(); } 

#if FEATURE_COMINTEROP 
        ////////////////////////////////////////////////////////////////////////////////
        // This will return a class based upon the progID.  This is provided for
        // COM classic support.  Program ID's are not used in COM+ because they
        // have been superceded by namespace.  (This routine is called this instead 
        // of getClass() because of the name conflict with the first method above.)
        // 
        //   param progID:     the progID of the class to retrieve 
        //   returns:          the class object associated to the progID
        //// 
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static Type GetTypeFromProgID(String progID)
        {
                return RuntimeType.GetTypeFromProgIDImpl(progID, null, false); 
        }
 
        //////////////////////////////////////////////////////////////////////////////// 
        // This will return a class based upon the progID.  This is provided for
        // COM classic support.  Program ID's are not used in COM+ because they 
        // have been superceded by namespace.  (This routine is called this instead
        // of getClass() because of the name conflict with the first method above.)
        //
        //   param progID:     the progID of the class to retrieve 
        //   returns:          the class object associated to the progID
        //// 
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] 
        public static Type GetTypeFromProgID(String progID, bool throwOnError)
        { 
                return RuntimeType.GetTypeFromProgIDImpl(progID, null, throwOnError);
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] 
        public static Type GetTypeFromProgID(String progID, String server)
        { 
                return RuntimeType.GetTypeFromProgIDImpl(progID, server, false); 
        }
 
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static Type GetTypeFromProgID(String progID, String server, bool throwOnError)
        {
                return RuntimeType.GetTypeFromProgIDImpl(progID, server, throwOnError); 
        }
 
        //////////////////////////////////////////////////////////////////////////////// 
        // This will return a class based upon the CLSID.  This is provided for
        // COM classic support. 
        //
        //   param CLSID:      the CLSID of the class to retrieve
        //   returns:          the class object associated to the CLSID
        //// 
        public static Type GetTypeFromCLSID(Guid clsid)
        { 
                return RuntimeType.GetTypeFromCLSIDImpl(clsid, null, false); 
        }
 
        public static Type GetTypeFromCLSID(Guid clsid, bool throwOnError)
        {
                return RuntimeType.GetTypeFromCLSIDImpl(clsid, null, throwOnError);
        } 

        public static Type GetTypeFromCLSID(Guid clsid, String server) 
        { 
                return RuntimeType.GetTypeFromCLSIDImpl(clsid, server, false);
        } 

        public static Type GetTypeFromCLSID(Guid clsid, String server, bool throwOnError)
        {
                return RuntimeType.GetTypeFromCLSIDImpl(clsid, server, throwOnError); 
        }
#endif // FEATURE_COMINTEROP 
 
        internal string SigToString()
        { 
            Type elementType = this;

            while(elementType.HasElementType)
                elementType = elementType.GetElementType(); 

            if (elementType.IsNested) 
                return Name; 

            string sigToString = ToString(); 

            if (elementType.IsPrimitive ||
                elementType == typeof(void) ||
                elementType == typeof(TypedReference)) 
                sigToString = sigToString.Substring(@"System.".Length);
 
            return sigToString; 
        }
 
        // GetTypeCode
        // This method will return a TypeCode for the passed
        //  type.
        public static TypeCode GetTypeCode(Type type) 
        {
            if (type == null) 
                return TypeCode.Empty; 
            return type.GetTypeCodeInternal();
        } 

        internal virtual TypeCode GetTypeCodeInternal()
        {
            Type type = this; 
            if (type is SymbolType)
                return TypeCode.Object; 
 
            if (type is TypeBuilder)
            { 
                TypeBuilder typeBuilder = (TypeBuilder) type;
                if (typeBuilder.IsEnum == false)
                    return TypeCode.Object;
 
                // if it is an Enum, just let the underlyingSystemType do the work
            } 
 
            if (type != type.UnderlyingSystemType)
                return Type.GetTypeCode(type.UnderlyingSystemType); 

            return TypeCode.Object;
        }
 
        // Property representing the GUID associated with a class.
        public abstract Guid GUID { 
            get; 
        }
 
        // Return the Default binder used by the system.
        static public Binder DefaultBinder {
            get {
                // Allocate the default binder if it hasn't been allocated yet. 
                if (defaultBinder == null)
                    CreateBinder(); 
                return defaultBinder as Binder; 
            }
        } 

        static private void CreateBinder()
        {
            if (defaultBinder == null) 
            {
                object binder = new DefaultBinder(); 
                Interlocked.CompareExchange(ref defaultBinder, binder, null); 
            }
        } 

       // Description of the Binding Process.
       // We must invoke a method that is accessable and for which the provided
       // parameters have the most specific match.  A method may be called if 
       // 1. The number of parameters in the method declaration equals the number of
       //      arguments provided to the invocation 
       // 2. The type of each argument can be converted by the binder to the 
       //      type of the type of the parameter.
       // 
       // The binder will find all of the matching methods.  These method are found based
       // upon the type of binding requested (MethodInvoke, Get/Set Properties).  The set
       // of methods is filtered by the name, number of arguments and a set of search modifiers
       // defined in the Binder. 
       //
       // After the method is selected, it will be invoked.  Accessability is checked 
       // at that point.  The search may be control which set of methods are searched based 
       // upon the accessibility attribute associated with the method.
       // 
       // The BindToMethod method is responsible for selecting the method to be invoked.
       // For the default binder, the most specific method will be selected.
       //
       // This will invoke a specific member... 

        abstract public Object InvokeMember(String name,BindingFlags invokeAttr,Binder binder,Object target, 
                                    Object[] args, ParameterModifier[] modifiers,CultureInfo culture,String[] namedParameters); 

        [DebuggerStepThroughAttribute] 
        [Diagnostics.DebuggerHidden]
        public Object InvokeMember(String name,BindingFlags invokeAttr,Binder binder, Object target, Object[] args, CultureInfo culture)
        {
                return InvokeMember(name,invokeAttr,binder,target,args,null,culture,null); 
        }
 
        [DebuggerStepThroughAttribute] 
        [Diagnostics.DebuggerHidden]
        public Object InvokeMember(String name,BindingFlags invokeAttr,Binder binder, Object target, Object[] args) 
        {
                return InvokeMember(name,invokeAttr,binder,target,args,null,null,null);
        }
 

        // Module Property associated with a class. 
        public new abstract Module Module { get; } 

        // Assembly Property associated with a class. 
        public abstract Assembly Assembly {
            get;
        }
 
        // A class handle is a unique integer value associated with
        // each class.  The handle is unique during the process life time. 
        public virtual extern RuntimeTypeHandle TypeHandle { 
            [MethodImplAttribute(MethodImplOptions.InternalCall)]
            get; 
        }

        internal virtual RuntimeTypeHandle GetTypeHandleInternal() {
            return TypeHandle; 
        }
 
        [MethodImpl(MethodImplOptions.InternalCall)] 
        public static extern RuntimeTypeHandle GetTypeHandle(Object o);
 
        // Given a class handle, this will return the class for that handle.
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern Type GetTypeFromHandle(RuntimeTypeHandle handle);
 
        // Return the fully qualified name.  The name does contain the namespace.
        public abstract String FullName { 
            get; 
        }
 
        // Return the name space of the class.
        public abstract String Namespace {
            get;
        } 

 
        public abstract String AssemblyQualifiedName { 
            get;
        } 


        public virtual int GetArrayRank() {
            throw new NotSupportedException(Environment.GetResourceString("NotSupported_SubclassOverride")); 
        }
 
        // Returns the base class for a class.  If this is an interface or has 
        // no base class null is returned.  Object is the only Type that does not
        // have a base class. 
        public abstract Type BaseType {
            get;
        }
 

        // GetConstructor 
        // This method will search for the specified constructor.  For constructors, 
        //  unlike everything else, the default is to not look for static methods.  The
        //  reason is that we don't typically expose the class initializer. 
[System.Runtime.InteropServices.ComVisible(true)]
        public ConstructorInfo GetConstructor(BindingFlags bindingAttr,
                                              Binder binder,
                                              CallingConventions callConvention, 
                                              Type[] types,
                                              ParameterModifier[] modifiers) 
        { 
           // Must provide some types (Type[0] for nothing)
            if (types == null) 
                throw new ArgumentNullException("types");
            for (int i=0;i attrs = CustomAttributeData.GetCustomAttributes(t);
                    for (int i = 0; i < attrs.Count; i++) 
                    {
                        if (attrs[i].Constructor.DeclaringType == typeof(DefaultMemberAttribute)) 
                        { 
                            attr = attrs[i];
                            break; 
                        }
                    }

                    if (attr != null) 
                        break;
                } 
 
                if (attr == null)
                    return new MemberInfo[0]; 
                defaultMember = attr.ConstructorArguments[0].Value as string;
                this.Cache[CacheObjType.DefaultMember] = defaultMember;
            }
 
            MemberInfo[] members = GetMember(defaultMember);
            if (members == null) 
                members = new MemberInfo[0]; 
            return members;
        } 

        internal virtual String GetDefaultMemberName() {
                // See if we have cached the default member name
                String defaultMember = (String)this.Cache[CacheObjType.DefaultMember]; 

                if (defaultMember == null) 
                { 
                    Object[] attrs = GetCustomAttributes(typeof(DefaultMemberAttribute), true);
                    // We assume that there is only one DefaultMemberAttribute (Allow multiple = false) 
                    if (attrs.Length > 1)
                            throw new ExecutionEngineException(Environment.GetResourceString("ExecutionEngine_InvalidAttribute"));
                    if (attrs.Length == 0)
                            return null; 
                    defaultMember = ((DefaultMemberAttribute)attrs[0]).MemberName;
                    this.Cache[CacheObjType.DefaultMember] = defaultMember; 
                } 
                return defaultMember;
        } 

        // FindMembers
        // This will return a filtered version of the member information
        public virtual MemberInfo[] FindMembers(MemberTypes memberType,BindingFlags bindingAttr,MemberFilter filter,Object filterCriteria) 
        {
            // Define the work arrays 
            MethodInfo[] m = null; 
            ConstructorInfo[] c = null;
            FieldInfo[] f = null; 
            PropertyInfo[] p = null;
            EventInfo[] e = null;
            Type[] t = null;
 
            int i = 0;
            int cnt = 0;            // Total Matchs 
 
            // Check the methods
            if ((memberType & System.Reflection.MemberTypes.Method) != 0) { 
                m = GetMethods(bindingAttr);
                if (filter != null) {
                    for (i=0;i attrs = CustomAttributeData.GetCustomAttributes(t);
                    for (int i = 0; i < attrs.Count; i++) 
                    {
                        if (attrs[i].Constructor.DeclaringType == typeof(DefaultMemberAttribute)) 
                        { 
                            attr = attrs[i];
                            break; 
                        }
                    }

                    if (attr != null) 
                        break;
                } 
 
                if (attr == null)
                    return new MemberInfo[0]; 
                defaultMember = attr.ConstructorArguments[0].Value as string;
                this.Cache[CacheObjType.DefaultMember] = defaultMember;
            }
 
            MemberInfo[] members = GetMember(defaultMember);
            if (members == null) 
                members = new MemberInfo[0]; 
            return members;
        } 

        internal virtual String GetDefaultMemberName() {
                // See if we have cached the default member name
                String defaultMember = (String)this.Cache[CacheObjType.DefaultMember]; 

                if (defaultMember == null) 
                { 
                    Object[] attrs = GetCustomAttributes(typeof(DefaultMemberAttribute), true);
                    // We assume that there is only one DefaultMemberAttribute (Allow multiple = false) 
                    if (attrs.Length > 1)
                            throw new ExecutionEngineException(Environment.GetResourceString("ExecutionEngine_InvalidAttribute"));
                    if (attrs.Length == 0)
                            return null; 
                    defaultMember = ((DefaultMemberAttribute)attrs[0]).MemberName;
                    this.Cache[CacheObjType.DefaultMember] = defaultMember; 
                } 
                return defaultMember;
        } 

        // FindMembers
        // This will return a filtered version of the member information
        public virtual MemberInfo[] FindMembers(MemberTypes memberType,BindingFlags bindingAttr,MemberFilter filter,Object filterCriteria) 
        {
            // Define the work arrays 
            MethodInfo[] m = null; 
            ConstructorInfo[] c = null;
            FieldInfo[] f = null; 
            PropertyInfo[] p = null;
            EventInfo[] e = null;
            Type[] t = null;
 
            int i = 0;
            int cnt = 0;            // Total Matchs 
 
            // Check the methods
            if ((memberType & System.Reflection.MemberTypes.Method) != 0) { 
                m = GetMethods(bindingAttr);
                if (filter != null) {
                    for (i=0;i

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK