Esempio n. 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, false, assemblyQualifiedTypeName, true, false));
        }
Esempio n. 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, throwOnError, assemblyQualifiedTypeName, false, ignoreCase));
        }
Esempio n. 3
0
        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.ReadCompressedInt();
                if (fm.UnmanagedType == UnmanagedType.LPArray)
                {
                    fm.ArraySubType = (UnmanagedType)blob.ReadCompressedInt();
                    if (fm.ArraySubType == NATIVE_TYPE_MAX)
                    {
                        fm.ArraySubType = null;
                    }
                    if (blob.Length != 0)
                    {
                        fm.SizeParamIndex = (short)blob.ReadCompressedInt();
                        if (blob.Length != 0)
                        {
                            fm.SizeConst = blob.ReadCompressedInt();
                            if (blob.Length != 0 && blob.ReadCompressedInt() == 0)
                            {
                                fm.SizeParamIndex = null;
                            }
                        }
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.SafeArray)
                {
                    if (blob.Length != 0)
                    {
                        fm.SafeArraySubType = (VarEnum)blob.ReadCompressedInt();
                        if (blob.Length != 0)
                        {
                            fm.SafeArrayUserDefinedSubType = ReadType(module, blob);
                        }
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.ByValArray)
                {
                    fm.SizeConst = blob.ReadCompressedInt();
                    if (blob.Length != 0)
                    {
                        fm.ArraySubType = (UnmanagedType)blob.ReadCompressedInt();
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.ByValTStr)
                {
                    fm.SizeConst = blob.ReadCompressedInt();
                }
                else if (fm.UnmanagedType == UnmanagedType.Interface ||
                         fm.UnmanagedType == UnmanagedType.IDispatch ||
                         fm.UnmanagedType == UnmanagedType.IUnknown)
                {
                    if (blob.Length != 0)
                    {
                        fm.IidParameterIndex = blob.ReadCompressedInt();
                    }
                }
                else if (fm.UnmanagedType == UnmanagedType.CustomMarshaler)
                {
                    blob.ReadCompressedInt();
                    blob.ReadCompressedInt();
                    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.Assembly, false, fm.MarshalType, false, false);
                    }
                }
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        internal static CustomAttributeData GetMarshalAsAttribute(Module module, int token)
        {
            // TODO use binary search?
            for (int i = 0; i < module.FieldMarshal.records.Length; i++)
            {
                if (module.FieldMarshal.records[i].Parent == token)
                {
                    ByteReader    blob                        = module.GetBlob(module.FieldMarshal.records[i].NativeType);
                    UnmanagedType unmanagedType               = (UnmanagedType)blob.ReadCompressedInt();
                    UnmanagedType?arraySubType                = null;
                    short?        sizeParamIndex              = null;
                    int?          sizeConst                   = null;
                    VarEnum?      safeArraySubType            = null;
                    Type          safeArrayUserDefinedSubType = null;
                    int?          iidParameterIndex           = null;
                    string        marshalType                 = null;
                    string        marshalCookie               = null;
                    Type          marshalTypeRef              = null;
                    if (unmanagedType == UnmanagedType.LPArray)
                    {
                        arraySubType = (UnmanagedType)blob.ReadCompressedInt();
                        if (arraySubType == NATIVE_TYPE_MAX)
                        {
                            arraySubType = null;
                        }
                        if (blob.Length != 0)
                        {
                            sizeParamIndex = (short)blob.ReadCompressedInt();
                            if (blob.Length != 0)
                            {
                                sizeConst = blob.ReadCompressedInt();
                                if (blob.Length != 0 && blob.ReadCompressedInt() == 0)
                                {
                                    sizeParamIndex = null;
                                }
                            }
                        }
                    }
                    else if (unmanagedType == UnmanagedType.SafeArray)
                    {
                        if (blob.Length != 0)
                        {
                            safeArraySubType = (VarEnum)blob.ReadCompressedInt();
                            if (blob.Length != 0)
                            {
                                safeArrayUserDefinedSubType = ReadType(module, blob);
                            }
                        }
                    }
                    else if (unmanagedType == UnmanagedType.ByValArray)
                    {
                        sizeConst = blob.ReadCompressedInt();
                        if (blob.Length != 0)
                        {
                            arraySubType = (UnmanagedType)blob.ReadCompressedInt();
                        }
                    }
                    else if (unmanagedType == UnmanagedType.ByValTStr)
                    {
                        sizeConst = blob.ReadCompressedInt();
                    }
                    else if (unmanagedType == UnmanagedType.Interface ||
                             unmanagedType == UnmanagedType.IDispatch ||
                             unmanagedType == UnmanagedType.IUnknown)
                    {
                        if (blob.Length != 0)
                        {
                            iidParameterIndex = blob.ReadCompressedInt();
                        }
                    }
                    else if (unmanagedType == UnmanagedType.CustomMarshaler)
                    {
                        blob.ReadCompressedInt();
                        blob.ReadCompressedInt();
                        marshalType   = ReadString(blob);
                        marshalCookie = ReadString(blob);

                        TypeNameParser parser = TypeNameParser.Parse(marshalType, false);
                        if (!parser.Error)
                        {
                            marshalTypeRef = parser.GetType(module.universe, module.Assembly, false, marshalType, false);
                        }
                    }

                    Type typeofMarshalAs     = module.universe.System_Runtime_InteropServices_MarshalAsAttribute;
                    Type typeofUnmanagedType = module.universe.System_Runtime_InteropServices_UnmanagedType;
                    Type typeofVarEnum       = module.universe.System_Runtime_InteropServices_VarEnum;
                    Type typeofType          = module.universe.System_Type;
                    List <CustomAttributeNamedArgument> named = new List <CustomAttributeNamedArgument>();
                    if (arraySubType != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "ArraySubType", typeofUnmanagedType, arraySubType.Value);
                    }
                    if (sizeParamIndex != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "SizeParamIndex", module.universe.System_Int16, sizeParamIndex.Value);
                    }
                    if (sizeConst != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "SizeConst", module.universe.System_Int32, sizeConst.Value);
                    }
                    if (safeArraySubType != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "SafeArraySubType", typeofVarEnum, safeArraySubType.Value);
                    }
                    if (safeArrayUserDefinedSubType != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "SafeArrayUserDefinedSubType", typeofType, safeArrayUserDefinedSubType);
                    }
                    if (iidParameterIndex != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "IidParameterIndex", module.universe.System_Int32, iidParameterIndex.Value);
                    }
                    if (marshalType != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "MarshalType", module.universe.System_String, marshalType);
                    }
                    if (marshalTypeRef != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "MarshalTypeRef", module.universe.System_Type, marshalTypeRef);
                    }
                    if (marshalCookie != null)
                    {
                        AddNamedArgument(named, typeofMarshalAs, "MarshalCookie", module.universe.System_String, marshalCookie);
                    }
                    ConstructorInfo constructor = typeofMarshalAs.GetPseudoCustomAttributeConstructor(typeofUnmanagedType);
                    return(new CustomAttributeData(module, constructor, new object[] { unmanagedType }, named));
                }
            }
            throw new BadImageFormatException();
        }