Пример #1
0
        // this is similar to GetType(Assembly context, string assemblyQualifiedTypeName, bool throwOnError),
        // but instead it assumes that the type must exist (i.e. if EnableMissingMemberResolution is enabled
        // it will create a missing type)
        public Type ResolveType(Assembly context, string assemblyQualifiedTypeName)
        {
            TypeNameParser parser = TypeNameParser.Parse(assemblyQualifiedTypeName, false);

            if (parser.Error)
            {
                return(null);
            }
            return(parser.GetType(this, context == null ? null : context.ManifestModule, false, assemblyQualifiedTypeName, true, false));
        }
Пример #2
0
        // note that context is slightly different from the calling assembly (System.Type.GetType),
        // because context is passed to the AssemblyResolve event as the RequestingAssembly
        public Type GetType(Assembly context, string assemblyQualifiedTypeName, bool throwOnError, bool ignoreCase)
        {
            TypeNameParser parser = TypeNameParser.Parse(assemblyQualifiedTypeName, throwOnError);

            if (parser.Error)
            {
                return(null);
            }
            return(parser.GetType(this, context == null ? null : context.ManifestModule, throwOnError, assemblyQualifiedTypeName, false, ignoreCase));
        }
Пример #3
0
        internal Type GetMissingTypeOrThrow(Module requester, Module module, Type declaringType, TypeName typeName)
        {
            if (resolveMissingMembers || module.Assembly.__IsMissing)
            {
                return(GetMissingType(requester, module, declaringType, typeName));
            }
            string fullName = TypeNameParser.Escape(typeName.ToString());

            if (declaringType != null)
            {
                fullName = declaringType.FullName + "+" + fullName;
            }
            throw new TypeLoadException(String.Format("Type '{0}' not found in assembly '{1}'", fullName, module.Assembly.FullName));
        }
Пример #4
0
        private static Type ReadType(Module context, ByteReader br)
        {
            string typeName = br.ReadString();

            if (typeName == null)
            {
                return(null);
            }
            if (typeName.Length > 0 && typeName[typeName.Length - 1] == 0)
            {
                // there are broken compilers that emit an extra NUL character after the type name
                typeName = typeName.Substring(0, typeName.Length - 1);
            }
            return(TypeNameParser.Parse(typeName, true).GetType(context.universe, context, true, typeName, true, false));
        }
        internal static bool ReadFieldMarshal(Module module, int token, out FieldMarshal fm)
        {
            fm = new FieldMarshal();
            foreach (int i in module.FieldMarshal.Filter(token))
            {
                ByteReader blob = module.GetBlob(module.FieldMarshal.records[i].NativeType);
                fm.UnmanagedType = (UnmanagedType)blob.ReadCompressedUInt();
                if (fm.UnmanagedType == UnmanagedType.LPArray)
                {
                    fm.ArraySubType = (UnmanagedType)blob.ReadCompressedUInt();
                    if (fm.ArraySubType == NATIVE_TYPE_MAX)
                    {
                        fm.ArraySubType = null;
                    }
                    if (blob.Length != 0)
                    {
                        fm.SizeParamIndex = (short)blob.ReadCompressedUInt();
                        if (blob.Length != 0)
                        {
                            fm.SizeConst = blob.ReadCompressedUInt();
                            if (blob.Length != 0 && blob.ReadCompressedUInt() == 0)
                            {
                                fm.SizeParamIndex = null;
                            }
                        }
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.SafeArray)
                {
                    if (blob.Length != 0)
                    {
                        fm.SafeArraySubType = (VarEnum)blob.ReadCompressedUInt();
                        if (blob.Length != 0)
                        {
                            fm.SafeArrayUserDefinedSubType = ReadType(module, blob);
                        }
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.ByValArray)
                {
                    fm.SizeConst = blob.ReadCompressedUInt();
                    if (blob.Length != 0)
                    {
                        fm.ArraySubType = (UnmanagedType)blob.ReadCompressedUInt();
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.ByValTStr)
                {
                    fm.SizeConst = blob.ReadCompressedUInt();
                }
                else if (fm.UnmanagedType == UnmanagedType.Interface ||
                         fm.UnmanagedType == UnmanagedType.IDispatch ||
                         fm.UnmanagedType == UnmanagedType.IUnknown)
                {
                    if (blob.Length != 0)
                    {
                        fm.IidParameterIndex = blob.ReadCompressedUInt();
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType_CustomMarshaler)
                {
                    blob.ReadCompressedUInt();
                    blob.ReadCompressedUInt();
                    fm.MarshalType   = ReadString(blob);
                    fm.MarshalCookie = ReadString(blob);

                    TypeNameParser parser = TypeNameParser.Parse(fm.MarshalType, false);
                    if (!parser.Error)
                    {
                        fm.MarshalTypeRef = parser.GetType(module.universe, module, false, fm.MarshalType, false, false);
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #6
0
        internal Type Expand(Type type, Module context, bool throwOnError, string originalName, bool resolve, bool ignoreCase)
        {
            Debug.Assert(!resolve || !ignoreCase);
            if (type == null)
            {
                if (throwOnError)
                {
                    throw new TypeLoadException(originalName);
                }
                return(null);
            }
            if (nested != null)
            {
                Type outer;
                foreach (string nest in nested)
                {
                    outer = type;
                    TypeName name = TypeName.Split(TypeNameParser.Unescape(nest));
                    type = ignoreCase
                        ? outer.FindNestedTypeIgnoreCase(name.ToLowerInvariant())
                        : outer.FindNestedType(name);
                    if (type == null)
                    {
                        if (resolve)
                        {
                            type = outer.Module.universe.GetMissingTypeOrThrow(context, outer.Module, outer, name);
                        }
                        else if (throwOnError)
                        {
                            throw new TypeLoadException(originalName);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
            }
            if (genericParameters != null)
            {
                Type[] typeArgs = new Type[genericParameters.Length];
                for (int i = 0; i < typeArgs.Length; i++)
                {
                    typeArgs[i] = genericParameters[i].GetType(type.Assembly.universe, context, throwOnError, originalName, resolve, ignoreCase);
                    if (typeArgs[i] == null)
                    {
                        return(null);
                    }
                }
                type = type.MakeGenericType(typeArgs);
            }
            if (modifiers != null)
            {
                foreach (short modifier in modifiers)
                {
                    switch (modifier)
                    {
                    case SZARRAY:
                        type = type.MakeArrayType();
                        break;

                    case BYREF:
                        type = type.MakeByRefType();
                        break;

                    case POINTER:
                        type = type.MakePointerType();
                        break;

                    default:
                        type = type.MakeArrayType(modifier);
                        break;
                    }
                }
            }
            return(type);
        }