MakeGeneric() public static method

public static MakeGeneric ( Type t, List typeArgs ) : Type
t System.Type
typeArgs List
return System.Type
Ejemplo n.º 1
0
        static Type ReadType(IMetadataImport importer, ref IntPtr pData)
        {
            CorElementType et;

            unsafe {
                var pBytes = (byte *)pData;
                et    = (CorElementType)(*pBytes);
                pData = (IntPtr)(pBytes + 1);
            }

            switch (et)
            {
            case CorElementType.ELEMENT_TYPE_VOID: return(typeof(void));

            case CorElementType.ELEMENT_TYPE_BOOLEAN: return(typeof(bool));

            case CorElementType.ELEMENT_TYPE_CHAR: return(typeof(char));

            case CorElementType.ELEMENT_TYPE_I1: return(typeof(sbyte));

            case CorElementType.ELEMENT_TYPE_U1: return(typeof(byte));

            case CorElementType.ELEMENT_TYPE_I2: return(typeof(short));

            case CorElementType.ELEMENT_TYPE_U2: return(typeof(ushort));

            case CorElementType.ELEMENT_TYPE_I4: return(typeof(int));

            case CorElementType.ELEMENT_TYPE_U4: return(typeof(uint));

            case CorElementType.ELEMENT_TYPE_I8: return(typeof(long));

            case CorElementType.ELEMENT_TYPE_U8: return(typeof(ulong));

            case CorElementType.ELEMENT_TYPE_R4: return(typeof(float));

            case CorElementType.ELEMENT_TYPE_R8: return(typeof(double));

            case CorElementType.ELEMENT_TYPE_STRING: return(typeof(string));

            case CorElementType.ELEMENT_TYPE_I: return(typeof(IntPtr));

            case CorElementType.ELEMENT_TYPE_U: return(typeof(UIntPtr));

            case CorElementType.ELEMENT_TYPE_OBJECT: return(typeof(object));

            case CorElementType.ELEMENT_TYPE_VAR:
            case CorElementType.ELEMENT_TYPE_MVAR:
                // Generic args in methods not supported. Return a dummy type.
                MetadataHelperFunctions.CorSigUncompressData(ref pData);
                return(typeof(GenericType));

            case CorElementType.ELEMENT_TYPE_GENERICINST: {
                Type t        = ReadType(importer, ref pData);
                var  typeArgs = new List <Type> ();
                uint num      = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                for (int n = 0; n < num; n++)
                {
                    typeArgs.Add(ReadType(importer, ref pData));
                }
                return(MetadataExtensions.MakeGeneric(t, typeArgs));
            }

            case CorElementType.ELEMENT_TYPE_PTR: {
                Type t = ReadType(importer, ref pData);
                return(MetadataExtensions.MakePointer(t));
            }

            case CorElementType.ELEMENT_TYPE_BYREF: {
                Type t = ReadType(importer, ref pData);
                return(MetadataExtensions.MakeByRef(t));
            }

            case CorElementType.ELEMENT_TYPE_END:
            case CorElementType.ELEMENT_TYPE_VALUETYPE:
            case CorElementType.ELEMENT_TYPE_CLASS: {
                uint token = MetadataHelperFunctions.CorSigUncompressToken(ref pData);
                return(new MetadataType(importer, (int)token));
            }

            case CorElementType.ELEMENT_TYPE_ARRAY: {
                Type t    = ReadType(importer, ref pData);
                int  rank = (int)MetadataHelperFunctions.CorSigUncompressData(ref pData);
                if (rank == 0)
                {
                    return(MetadataExtensions.MakeArray(t, null, null));
                }

                uint numSizes = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  sizes    = new List <int> (rank);
                for (int n = 0; n < numSizes && n < rank; n++)
                {
                    sizes.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                uint numLoBounds = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  loBounds    = new List <int> (rank);
                for (int n = 0; n < numLoBounds && n < rank; n++)
                {
                    loBounds.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                return(MetadataExtensions.MakeArray(t, sizes, loBounds));
            }

            case CorElementType.ELEMENT_TYPE_SZARRAY: {
                Type t = ReadType(importer, ref pData);
                return(MetadataExtensions.MakeArray(t, null, null));
            }

            case CorElementType.ELEMENT_TYPE_FNPTR: {
                CorCallingConvention cconv;
                Type        retType;
                List <Type> argTypes;
                ReadMethodSignature(importer, ref pData, out cconv, out retType, out argTypes);
                return(MetadataExtensions.MakeDelegate(retType, argTypes));
            }

            case CorElementType.ELEMENT_TYPE_CMOD_REQD:
            case CorElementType.ELEMENT_TYPE_CMOD_OPT:
                return(ReadType(importer, ref pData));
            }
            throw new NotSupportedException("Unknown sig element type: " + et);
        }
Ejemplo n.º 2
0
        static Type ReadType(IMetadataImport importer, Instantiation instantiation, ref IntPtr pData)
        {
            CorElementType et;

            unsafe {
                var pBytes = (byte *)pData;
                et    = (CorElementType)(*pBytes);
                pData = (IntPtr)(pBytes + 1);
            }

            if ((et & CorElementType.ELEMENT_TYPE_SENTINEL) == CorElementType.ELEMENT_TYPE_SENTINEL)
            {
                et ^= CorElementType.ELEMENT_TYPE_SENTINEL;                 // substract SENTINEL bits from element type to get clean ET
            }

            switch (et)
            {
            case CorElementType.ELEMENT_TYPE_VOID: return(typeof(void));

            case CorElementType.ELEMENT_TYPE_BOOLEAN: return(typeof(bool));

            case CorElementType.ELEMENT_TYPE_CHAR: return(typeof(char));

            case CorElementType.ELEMENT_TYPE_I1: return(typeof(sbyte));

            case CorElementType.ELEMENT_TYPE_U1: return(typeof(byte));

            case CorElementType.ELEMENT_TYPE_I2: return(typeof(short));

            case CorElementType.ELEMENT_TYPE_U2: return(typeof(ushort));

            case CorElementType.ELEMENT_TYPE_I4: return(typeof(int));

            case CorElementType.ELEMENT_TYPE_U4: return(typeof(uint));

            case CorElementType.ELEMENT_TYPE_I8: return(typeof(long));

            case CorElementType.ELEMENT_TYPE_U8: return(typeof(ulong));

            case CorElementType.ELEMENT_TYPE_R4: return(typeof(float));

            case CorElementType.ELEMENT_TYPE_R8: return(typeof(double));

            case CorElementType.ELEMENT_TYPE_STRING: return(typeof(string));

            case CorElementType.ELEMENT_TYPE_I: return(typeof(IntPtr));

            case CorElementType.ELEMENT_TYPE_U: return(typeof(UIntPtr));

            case CorElementType.ELEMENT_TYPE_OBJECT: return(typeof(object));

            case CorElementType.ELEMENT_TYPE_TYPEDBYREF: return(typeof(TypedReference));

            case CorElementType.ELEMENT_TYPE_VAR: {
                var index = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                if (index < instantiation.TypeArgs.Count)
                {
                    return(instantiation.TypeArgs[(int)index]);
                }
                return(new TypeGenericParameter((int)index));
            }

            case CorElementType.ELEMENT_TYPE_MVAR: {
                // Generic args in methods not supported. Return a dummy type.
                var index = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                return(new MethodGenericParameter((int)index));
            }

            case CorElementType.ELEMENT_TYPE_GENERICINST: {
                Type t        = ReadType(importer, instantiation, ref pData);
                var  typeArgs = new List <Type> ();
                uint num      = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                for (int n = 0; n < num; n++)
                {
                    typeArgs.Add(ReadType(importer, instantiation, ref pData));
                }
                return(MetadataExtensions.MakeGeneric(t, typeArgs));
            }

            case CorElementType.ELEMENT_TYPE_PTR: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakePointer(t));
            }

            case CorElementType.ELEMENT_TYPE_BYREF: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakeByRef(t));
            }

            case CorElementType.ELEMENT_TYPE_END:
            case CorElementType.ELEMENT_TYPE_VALUETYPE:
            case CorElementType.ELEMENT_TYPE_CLASS: {
                uint token = MetadataHelperFunctions.CorSigUncompressToken(ref pData);
                return(new MetadataType(importer, (int)token));
            }

            case CorElementType.ELEMENT_TYPE_ARRAY: {
                Type t    = ReadType(importer, instantiation, ref pData);
                int  rank = (int)MetadataHelperFunctions.CorSigUncompressData(ref pData);
                if (rank == 0)
                {
                    return(MetadataExtensions.MakeArray(t, null, null));
                }

                uint numSizes = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  sizes    = new List <int> (rank);
                for (int n = 0; n < numSizes && n < rank; n++)
                {
                    sizes.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                uint numLoBounds = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  loBounds    = new List <int> (rank);
                for (int n = 0; n < numLoBounds && n < rank; n++)
                {
                    loBounds.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                return(MetadataExtensions.MakeArray(t, sizes, loBounds));
            }

            case CorElementType.ELEMENT_TYPE_SZARRAY: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakeArray(t, null, null));
            }

            case CorElementType.ELEMENT_TYPE_FNPTR: {
                CorCallingConvention cconv;
                Type        retType;
                List <Type> argTypes;
                int         sentinelIndex;
                ReadMethodSignature(importer, instantiation, ref pData, out cconv, out retType, out argTypes, out sentinelIndex);
                return(MetadataExtensions.MakeDelegate(retType, argTypes));
            }

            case CorElementType.ELEMENT_TYPE_CMOD_REQD:
            case CorElementType.ELEMENT_TYPE_CMOD_OPT: {
                uint token = MetadataHelperFunctions.CorSigUncompressToken(ref pData);
                return(new MetadataType(importer, (int)token));
            }

            case CorElementType.ELEMENT_TYPE_INTERNAL:
                return(typeof(object));                // hack to avoid the exceptions. CLR spec says that this type should never occurs, but it occurs sometimes, mystics

            case CorElementType.ELEMENT_TYPE_NATIVE_ARRAY_TEMPLATE_ZAPSIG:
            case CorElementType.ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG:
                return(ReadType(importer, instantiation, ref pData));

            case CorElementType.ELEMENT_TYPE_CANON_ZAPSIG:
                return(typeof(object));                // this is representation of __Canon type, but it's inaccessible, using object instead
            }
            throw new NotSupportedException("Unknown sig element type: " + et);
        }
        static Type ReadType(CorApi.Portable.IMetaDataImport importer, Instantiation instantiation, ref IntPtr pData)
        {
            CorApi.Portable.CorElementType et;
            unsafe {
                var pBytes = (byte *)pData;
                et    = (CorApi.Portable.CorElementType)(*pBytes);
                pData = (IntPtr)(pBytes + 1);
            }

            if ((et & CorApi.Portable.CorElementType.ElementTypeSentinel) == CorApi.Portable.CorElementType.ElementTypeSentinel)
            {
                et ^= CorApi.Portable.CorElementType.ElementTypeSentinel;                 // substract SENTINEL bits from element type to get clean ET
            }

            switch (et)
            {
            case CorApi.Portable.CorElementType.ElementTypeVoid: return(typeof(void));

            case CorApi.Portable.CorElementType.ElementTypeBoolean: return(typeof(bool));

            case CorApi.Portable.CorElementType.ElementTypeChar: return(typeof(char));

            case CorApi.Portable.CorElementType.ElementTypeI1: return(typeof(sbyte));

            case CorApi.Portable.CorElementType.ElementTypeU1: return(typeof(byte));

            case CorApi.Portable.CorElementType.ElementTypeI2: return(typeof(short));

            case CorApi.Portable.CorElementType.ElementTypeU2: return(typeof(ushort));

            case CorApi.Portable.CorElementType.ElementTypeI4: return(typeof(int));

            case CorApi.Portable.CorElementType.ElementTypeU4: return(typeof(uint));

            case CorApi.Portable.CorElementType.ElementTypeI8: return(typeof(long));

            case CorApi.Portable.CorElementType.ElementTypeU8: return(typeof(ulong));

            case CorApi.Portable.CorElementType.ElementTypeR4: return(typeof(float));

            case CorApi.Portable.CorElementType.ElementTypeR8: return(typeof(double));

            case CorApi.Portable.CorElementType.ElementTypeString: return(typeof(string));

            case CorApi.Portable.CorElementType.ElementTypeI: return(typeof(IntPtr));

            case CorApi.Portable.CorElementType.ElementTypeU: return(typeof(UIntPtr));

            case CorApi.Portable.CorElementType.ElementTypeObject: return(typeof(object));

            case CorApi.Portable.CorElementType.ElementTypeTypedbyref: return(typeof(TypedReference));

            case CorApi.Portable.CorElementType.ElementTypeVar: {
                var index = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                if (index < instantiation.TypeArgs.Count)
                {
                    return(instantiation.TypeArgs[(int)index]);
                }
                return(new TypeGenericParameter((int)index));
            }

            case CorApi.Portable.CorElementType.ElementTypeMvar: {
                // Generic args in methods not supported. Return a dummy type.
                var index = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                return(new MethodGenericParameter((int)index));
            }

            case CorApi.Portable.CorElementType.ElementTypeGenericinst: {
                Type t        = ReadType(importer, instantiation, ref pData);
                var  typeArgs = new List <Type> ();
                uint num      = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                for (int n = 0; n < num; n++)
                {
                    typeArgs.Add(ReadType(importer, instantiation, ref pData));
                }
                return(MetadataExtensions.MakeGeneric(t, typeArgs));
            }

            case CorApi.Portable.CorElementType.ElementTypePtr: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakePointer(t));
            }

            case CorApi.Portable.CorElementType.ElementTypeByref: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakeByRef(t));
            }

            case CorApi.Portable.CorElementType.ElementTypeEnd:
            case CorApi.Portable.CorElementType.ElementTypeValuetype:
            case CorApi.Portable.CorElementType.ElementTypeClass: {
                uint token = MetadataHelperFunctions.CorSigUncompressToken(ref pData);
                return(new MetadataType(importer, (int)token));
            }

            case CorApi.Portable.CorElementType.ElementTypeArray: {
                Type t    = ReadType(importer, instantiation, ref pData);
                int  rank = (int)MetadataHelperFunctions.CorSigUncompressData(ref pData);
                if (rank == 0)
                {
                    return(MetadataExtensions.MakeArray(t, null, null));
                }

                uint numSizes = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  sizes    = new List <int> (rank);
                for (int n = 0; n < numSizes && n < rank; n++)
                {
                    sizes.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                uint numLoBounds = MetadataHelperFunctions.CorSigUncompressData(ref pData);
                var  loBounds    = new List <int> (rank);
                for (int n = 0; n < numLoBounds && n < rank; n++)
                {
                    loBounds.Add((int)MetadataHelperFunctions.CorSigUncompressData(ref pData));
                }

                return(MetadataExtensions.MakeArray(t, sizes, loBounds));
            }

            case CorApi.Portable.CorElementType.ElementTypeSzarray: {
                Type t = ReadType(importer, instantiation, ref pData);
                return(MetadataExtensions.MakeArray(t, null, null));
            }

            case CorApi.Portable.CorElementType.ElementTypeFnptr: {
                CorCallingConvention cconv;
                Type        retType;
                List <Type> argTypes;
                int         sentinelIndex;
                ReadMethodSignature(importer, instantiation, ref pData, out cconv, out retType, out argTypes, out sentinelIndex);
                return(MetadataExtensions.MakeDelegate(retType, argTypes));
            }

            case CorApi.Portable.CorElementType.ElementTypeCmodReqd:
            case CorApi.Portable.CorElementType.ElementTypeCmodOptimization: {
                uint token = MetadataHelperFunctions.CorSigUncompressToken(ref pData);
                return(new MetadataType(importer, (int)token));
            }

            case CorApi.Portable.CorElementType.ElementTypeInternal:
                return(typeof(object));                // hack to avoid the exceptions. CLR spec says that this type should never occurs, but it occurs sometimes, mystics

            case (CorApi.Portable.CorElementType)CorApi.Portable.CorElementTypeExtra.ELEMENT_TYPE_NATIVE_ARRAY_TEMPLATE_ZAPSIG:
            case (CorApi.Portable.CorElementType)CorApi.Portable.CorElementTypeExtra.ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG:
                return(ReadType(importer, instantiation, ref pData));

            case (CorApi.Portable.CorElementType)CorApi.Portable.CorElementTypeExtra.ELEMENT_TYPE_CANON_ZAPSIG:
                return(typeof(object));                // this is representation of __Canon type, but it's inaccessible, using object instead
            }
            throw new NotSupportedException("Unknown sig element type: " + et);
        }