Ejemplo n.º 1
0
        internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
        {
            int        sizeParamIndex = 0;
            int        sizeConst      = 0;
            string     marshalType    = (string)null;
            string     marshalCookie  = (string)null;
            string     safeArrayUserDefinedSubType1 = (string)null;
            int        iidParamIndex = 0;
            ConstArray fieldMarshal  = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);

            if (fieldMarshal.Length == 0)
            {
                return((Attribute)null);
            }
            UnmanagedType unmanagedType;
            VarEnum       safeArraySubType;
            UnmanagedType arraySubType;

            MetadataImport.GetMarshalAs(fieldMarshal, out unmanagedType, out safeArraySubType, out safeArrayUserDefinedSubType1, out arraySubType, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie, out iidParamIndex);
            RuntimeType safeArrayUserDefinedSubType2 = safeArrayUserDefinedSubType1 == null || safeArrayUserDefinedSubType1.Length == 0 ? (RuntimeType)null : RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedSubType1, scope);
            RuntimeType marshalTypeRef = (RuntimeType)null;

            try
            {
                marshalTypeRef = marshalType == null ? (RuntimeType)null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalType, scope);
            }
            catch (TypeLoadException ex)
            {
            }
            return((Attribute) new MarshalAsAttribute(unmanagedType, safeArraySubType, safeArrayUserDefinedSubType2, arraySubType, (short)sizeParamIndex, sizeConst, marshalType, marshalTypeRef, marshalCookie, iidParamIndex));
        }
Ejemplo n.º 2
0
        internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
        {
            int        num           = 0;
            int        sizeConst     = 0;
            string     text          = null;
            string     marshalCookie = null;
            string     text2         = null;
            int        iidParamIndex = 0;
            ConstArray fieldMarshal  = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);

            if (fieldMarshal.Length == 0)
            {
                return(null);
            }
            UnmanagedType val;
            VarEnum       safeArraySubType;
            UnmanagedType arraySubType;

            MetadataImport.GetMarshalAs(fieldMarshal, out val, out safeArraySubType, out text2, out arraySubType, out num, out sizeConst, out text, out marshalCookie, out iidParamIndex);
            RuntimeType safeArrayUserDefinedSubType = (text2 == null || text2.Length == 0) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(text2, scope);
            RuntimeType marshalTypeRef = null;

            try
            {
                marshalTypeRef = ((text == null) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(text, scope));
            }
            catch (TypeLoadException)
            {
            }
            return(new MarshalAsAttribute(val, safeArraySubType, safeArrayUserDefinedSubType, arraySubType, (short)num, sizeConst, text, marshalTypeRef, marshalCookie, iidParamIndex));
        }
        internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
        {
            UnmanagedType type;
            UnmanagedType type2;
            VarEnum       enum2;
            int           sizeParamIndex = 0;
            int           sizeConst      = 0;
            string        marshalType    = null;
            string        marshalCookie  = null;
            string        safeArrayUserDefinedSubType = null;
            int           iidParamIndex = 0;
            ConstArray    fieldMarshal  = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);

            if (fieldMarshal.Length == 0)
            {
                return(null);
            }
            MetadataImport.GetMarshalAs(fieldMarshal, out type, out enum2, out safeArrayUserDefinedSubType, out type2, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie, out iidParamIndex);
            RuntimeType type3          = ((safeArrayUserDefinedSubType == null) || (safeArrayUserDefinedSubType.Length == 0)) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedSubType, scope);
            RuntimeType marshalTypeRef = null;

            try
            {
                marshalTypeRef = (marshalType == null) ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalType, scope);
            }
            catch (TypeLoadException)
            {
            }
            return(new MarshalAsAttribute(type, enum2, type3, type2, (short)sizeParamIndex, sizeConst, marshalType, marshalTypeRef, marshalCookie, iidParamIndex));
        }
Ejemplo n.º 4
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      Environment.GetResourceString("Argument_InvalidToken", tk, this));
            }

            RuntimeTypeHandle[] typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                IRuntimeFieldInfo fieldHandle = null;

                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken",
                                                    Environment.GetResourceString("Argument_ResolveField", tk, this));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature.ToPointer() != MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException("metadataToken",
                                                        Environment.GetResourceString("Argument_ResolveField", tk, this));
                        }
                    }

                    fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
                }

                fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), metadataToken, typeArgs, methodArgs);
                RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = ModuleHandle.GetMetadataImport(GetNativeHandle()).GetParentToken(metadataToken);
                    declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(System.RuntimeType.GetFieldInfo(declaringType, fieldHandle));
            }
            catch (MissingFieldException)
            {
                return(ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }
        }
Ejemplo n.º 5
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
            {
                return(null);
            }

            MetadataImport    scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            entryPoint, dllName = null;
            int               token = method.MetadataToken;
            PInvokeAttributes flags = 0;

            scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);

            CharSet charSet = CharSet.None;

            switch (flags & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;

            case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;

            case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;

            case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;

            // Invalid: default to CharSet.None
            default: break;
            }

            CallingConvention callingConvention = CallingConvention.Cdecl;

            switch (flags & PInvokeAttributes.CallConvMask)
            {
            case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;

            case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;

            case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;

            case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;

            case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;

            // Invalid: default to CallingConvention.Cdecl
            default: break;
            }

            bool exactSpelling         = (flags & PInvokeAttributes.NoMangle) != 0;
            bool setLastError          = (flags & PInvokeAttributes.SupportsLastError) != 0;
            bool bestFitMapping        = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;

            return(new DllImportAttribute(
                       dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
                       callingConvention, bestFitMapping, throwOnUnmappableChar));
        }
Ejemplo n.º 6
0
        public unsafe override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken metadataToken2 = new MetadataToken(metadataToken);

            if (!this.MetadataImport.IsValidToken(metadataToken2))
            {
                throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", new object[]
                {
                    metadataToken2,
                    this
                }));
            }
            RuntimeTypeHandle[] typeInstantiationContext   = RuntimeModule.ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodInstantiationContext = RuntimeModule.ConvertToTypeHandleArray(genericMethodArguments);
            FieldInfo           result;

            try
            {
                IRuntimeFieldInfo runtimeFieldInfo;
                if (!metadataToken2.IsFieldDef)
                {
                    if (!metadataToken2.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[]
                        {
                            metadataToken2,
                            this
                        }));
                    }
                    if (*(byte *)this.MetadataImport.GetMemberRefProps(metadataToken2).Signature.ToPointer() != 6)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[]
                        {
                            metadataToken2,
                            this
                        }));
                    }
                    runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken2, typeInstantiationContext, methodInstantiationContext);
                }
                runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken, typeInstantiationContext, methodInstantiationContext);
                RuntimeType runtimeType = RuntimeFieldHandle.GetApproxDeclaringType(runtimeFieldInfo.Value);
                if (runtimeType.IsGenericType || runtimeType.IsArray)
                {
                    int parentToken = ModuleHandle.GetMetadataImport(this.GetNativeHandle()).GetParentToken(metadataToken);
                    runtimeType = (RuntimeType)this.ResolveType(parentToken, genericTypeArguments, genericMethodArguments);
                }
                result = RuntimeType.GetFieldInfo(runtimeType, runtimeFieldInfo);
            }
            catch (MissingFieldException)
            {
                result = this.ResolveLiteralField(metadataToken2, genericTypeArguments, genericMethodArguments);
            }
            catch (BadImageFormatException innerException)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), innerException);
            }
            return(result);
        }
Ejemplo n.º 7
0
        public override unsafe FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            FieldInfo fieldInfo;

            System.Reflection.MetadataToken token = new System.Reflection.MetadataToken(metadataToken);
            if (!this.MetadataImport.IsValidToken((int)token))
            {
                throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", new object[] { token, this }));
            }
            RuntimeTypeHandle[] typeInstantiationContext   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodInstantiationContext = ConvertToTypeHandleArray(genericMethodArguments);
            try
            {
                IRuntimeFieldInfo field = null;
                if (!token.IsFieldDef)
                {
                    if (!token.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[] { token, this }));
                    }
                    if (*(((byte *)this.MetadataImport.GetMemberRefProps((int)token).Signature.ToPointer())) != 6)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[] { token, this }));
                    }
                    field = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), (int)token, typeInstantiationContext, methodInstantiationContext);
                }
                field = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken, typeInstantiationContext, methodInstantiationContext);
                Type approxDeclaringType = RuntimeFieldHandle.GetApproxDeclaringType(field.Value);
                if (approxDeclaringType.IsGenericType || approxDeclaringType.IsArray)
                {
                    int parentToken = ModuleHandle.GetMetadataImport(this.GetNativeHandle()).GetParentToken(metadataToken);
                    approxDeclaringType = this.ResolveType(parentToken, genericTypeArguments, genericMethodArguments);
                }
                fieldInfo = System.RuntimeType.GetFieldInfo(approxDeclaringType.GetTypeHandleInternal().GetRuntimeType(), field);
            }
            catch (MissingFieldException)
            {
                fieldInfo = this.ResolveLiteralField((int)token, genericTypeArguments, genericMethodArguments);
            }
            catch (BadImageFormatException exception)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), exception);
            }
            return(fieldInfo);
        }
Ejemplo n.º 8
0
 public override unsafe FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
 {
     System.Reflection.MetadataToken metadataToken1 = new System.Reflection.MetadataToken(metadataToken);
     if (!this.MetadataImport.IsValidToken((int)metadataToken1))
     {
         throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", (object)metadataToken1, (object)this));
     }
     RuntimeTypeHandle[] typeHandleArray1 = RuntimeModule.ConvertToTypeHandleArray(genericTypeArguments);
     RuntimeTypeHandle[] typeHandleArray2 = RuntimeModule.ConvertToTypeHandleArray(genericMethodArguments);
     try
     {
         IRuntimeFieldInfo runtimeFieldInfo = (IRuntimeFieldInfo)null;
         if (!metadataToken1.IsFieldDef)
         {
             if (!metadataToken1.IsMemberRef)
             {
                 throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", (object)metadataToken1, (object)this));
             }
             if ((int)*(byte *)this.MetadataImport.GetMemberRefProps((int)metadataToken1).Signature.ToPointer() != 6)
             {
                 throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", (object)metadataToken1, (object)this));
             }
             runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), (int)metadataToken1, typeHandleArray1, typeHandleArray2);
         }
         IRuntimeFieldInfo field         = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken, typeHandleArray1, typeHandleArray2);
         RuntimeType       reflectedType = RuntimeFieldHandle.GetApproxDeclaringType(field.Value);
         if (reflectedType.IsGenericType || reflectedType.IsArray)
         {
             reflectedType = (RuntimeType)this.ResolveType(ModuleHandle.GetMetadataImport(this.GetNativeHandle()).GetParentToken(metadataToken), genericTypeArguments, genericMethodArguments);
         }
         return(RuntimeType.GetFieldInfo(reflectedType, field));
     }
     catch (MissingFieldException ex)
     {
         return(this.ResolveLiteralField((int)metadataToken1, genericTypeArguments, genericMethodArguments));
     }
     catch (BadImageFormatException ex)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), (Exception)ex);
     }
 }
Ejemplo n.º 9
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
        {
            UnmanagedType unmanagedType, arraySubType;
            VarEnum       safeArraySubType;
            int           sizeParamIndex = 0, sizeConst = 0;
            string        marshalTypeName = null, marshalCookie = null, safeArrayUserDefinedTypeName = null;
            int           iidParamIndex = 0;
            ConstArray    nativeType    = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);

            if (nativeType.Length == 0)
            {
                return(null);
            }

            MetadataImport.GetMarshalAs(nativeType,
                                        out unmanagedType, out safeArraySubType, out safeArrayUserDefinedTypeName, out arraySubType, out sizeParamIndex,
                                        out sizeConst, out marshalTypeName, out marshalCookie, out iidParamIndex);

            RuntimeType safeArrayUserDefinedType = safeArrayUserDefinedTypeName == null || safeArrayUserDefinedTypeName.Length == 0 ? null :
                                                   RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName, scope);
            RuntimeType marshalTypeRef = null;

            try
            {
                marshalTypeRef = marshalTypeName == null ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalTypeName, scope);
            }
            catch (System.TypeLoadException)
            {
                // The user may have supplied a bad type name string causing this TypeLoadException
                // Regardless, we return the bad type name
                Contract.Assert(marshalTypeName != null);
            }

            return(new MarshalAsAttribute(
                       unmanagedType, safeArraySubType, safeArrayUserDefinedType, arraySubType,
                       (short)sizeParamIndex, sizeConst, marshalTypeName, marshalTypeRef, marshalCookie, iidParamIndex));
        }
Ejemplo n.º 10
0
        public override FieldInfo?ResolveField(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            try
            {
                MetadataToken tk = new MetadataToken(metadataToken);

                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                          SR.Format(SR.Argument_InvalidToken, tk, this));
                }

                RuntimeTypeHandle[]? typeArgs   = null;
                RuntimeTypeHandle[]? methodArgs = null;
                if (genericTypeArguments?.Length > 0)
                {
                    typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
                }
                if (genericMethodArguments?.Length > 0)
                {
                    methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
                }

                ModuleHandle moduleHandle = new ModuleHandle(this);
                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                    nameof(metadataToken));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature != MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                        nameof(metadataToken));
                        }
                    }
                }

                IRuntimeFieldInfo fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo();

                RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = ModuleHandle.GetMetadataImport(this).GetParentToken(metadataToken);
                    declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(RuntimeType.GetFieldInfo(declaringType, fieldHandle));
            }
            catch (MissingFieldException)
            {
                return(ResolveLiteralField(metadataToken, genericTypeArguments, genericMethodArguments));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return(null);
            }
            MetadataImport    metadataImport    = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            dllName           = null;
            int               metadataToken     = method.MetadataToken;
            PInvokeAttributes pinvokeAttributes = PInvokeAttributes.CharSetNotSpec;
            string            entryPoint;

            metadataImport.GetPInvokeMap(metadataToken, out pinvokeAttributes, out entryPoint, out dllName);
            CharSet charSet = CharSet.None;

            switch (pinvokeAttributes & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec:
                charSet = CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                charSet = CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                charSet = CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetMask:
                charSet = CharSet.Auto;
                break;
            }
            CallingConvention callingConvention  = CallingConvention.Cdecl;
            PInvokeAttributes pinvokeAttributes2 = pinvokeAttributes & PInvokeAttributes.CallConvMask;

            if (pinvokeAttributes2 <= PInvokeAttributes.CallConvCdecl)
            {
                if (pinvokeAttributes2 != PInvokeAttributes.CallConvWinapi)
                {
                    if (pinvokeAttributes2 == PInvokeAttributes.CallConvCdecl)
                    {
                        callingConvention = CallingConvention.Cdecl;
                    }
                }
                else
                {
                    callingConvention = CallingConvention.Winapi;
                }
            }
            else if (pinvokeAttributes2 != PInvokeAttributes.CallConvStdcall)
            {
                if (pinvokeAttributes2 != PInvokeAttributes.CallConvThiscall)
                {
                    if (pinvokeAttributes2 == PInvokeAttributes.CallConvFastcall)
                    {
                        callingConvention = CallingConvention.FastCall;
                    }
                }
                else
                {
                    callingConvention = CallingConvention.ThisCall;
                }
            }
            else
            {
                callingConvention = CallingConvention.StdCall;
            }
            bool exactSpelling         = (pinvokeAttributes & PInvokeAttributes.NoMangle) > PInvokeAttributes.CharSetNotSpec;
            bool setLastError          = (pinvokeAttributes & PInvokeAttributes.SupportsLastError) > PInvokeAttributes.CharSetNotSpec;
            bool bestFitMapping        = (pinvokeAttributes & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (pinvokeAttributes & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) > MethodImplAttributes.IL;

            return(new DllImportAttribute(dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig, callingConvention, bestFitMapping, throwOnUnmappableChar));
        }
Ejemplo n.º 12
0
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            string str;

            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return(null);
            }
            MetadataImport    metadataImport  = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            importDll       = null;
            int               metadataToken   = method.MetadataToken;
            PInvokeAttributes bestFitUseAssem = PInvokeAttributes.BestFitUseAssem;

            metadataImport.GetPInvokeMap(metadataToken, out bestFitUseAssem, out str, out importDll);
            System.Runtime.InteropServices.CharSet none = System.Runtime.InteropServices.CharSet.None;
            switch ((bestFitUseAssem & PInvokeAttributes.CharSetAuto))
            {
            case PInvokeAttributes.BestFitUseAssem:
                none = System.Runtime.InteropServices.CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                none = System.Runtime.InteropServices.CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                none = System.Runtime.InteropServices.CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetAuto:
                none = System.Runtime.InteropServices.CharSet.Auto;
                break;
            }
            System.Runtime.InteropServices.CallingConvention cdecl = System.Runtime.InteropServices.CallingConvention.Cdecl;
            switch ((bestFitUseAssem & PInvokeAttributes.CallConvMask))
            {
            case PInvokeAttributes.CallConvStdcall:
                cdecl = System.Runtime.InteropServices.CallingConvention.StdCall;
                break;

            case PInvokeAttributes.CallConvThiscall:
                cdecl = System.Runtime.InteropServices.CallingConvention.ThisCall;
                break;

            case PInvokeAttributes.CallConvFastcall:
                cdecl = System.Runtime.InteropServices.CallingConvention.FastCall;
                break;

            case PInvokeAttributes.CallConvWinapi:
                cdecl = System.Runtime.InteropServices.CallingConvention.Winapi;
                break;

            case PInvokeAttributes.CallConvCdecl:
                cdecl = System.Runtime.InteropServices.CallingConvention.Cdecl;
                break;
            }
            bool exactSpelling         = (bestFitUseAssem & PInvokeAttributes.NoMangle) != PInvokeAttributes.BestFitUseAssem;
            bool setLastError          = (bestFitUseAssem & PInvokeAttributes.SupportsLastError) != PInvokeAttributes.BestFitUseAssem;
            bool bestFitMapping        = (bestFitUseAssem & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (bestFitUseAssem & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;

            return(new DllImportAttribute(importDll, str, none, exactSpelling, setLastError, (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL, cdecl, bestFitMapping, throwOnUnmappableChar));
        }
Ejemplo n.º 13
0
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return((Attribute)null);
            }
            MetadataImport    metadataImport = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            importDll      = (string)null;
            int               metadataToken  = method.MetadataToken;
            PInvokeAttributes attributes     = PInvokeAttributes.CharSetNotSpec;
            string            importName;

            metadataImport.GetPInvokeMap(metadataToken, out attributes, out importName, out importDll);
            CharSet charSet = CharSet.None;

            switch (attributes & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec:
                charSet = CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                charSet = CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                charSet = CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetMask:
                charSet = CharSet.Auto;
                break;
            }
            CallingConvention callingConvention = CallingConvention.Cdecl;

            switch (attributes & PInvokeAttributes.CallConvMask)
            {
            case PInvokeAttributes.CallConvStdcall:
                callingConvention = CallingConvention.StdCall;
                break;

            case PInvokeAttributes.CallConvThiscall:
                callingConvention = CallingConvention.ThisCall;
                break;

            case PInvokeAttributes.CallConvFastcall:
                callingConvention = CallingConvention.FastCall;
                break;

            case PInvokeAttributes.CallConvWinapi:
                callingConvention = CallingConvention.Winapi;
                break;

            case PInvokeAttributes.CallConvCdecl:
                callingConvention = CallingConvention.Cdecl;
                break;
            }
            bool exactSpelling         = (uint)(attributes & PInvokeAttributes.NoMangle) > 0U;
            bool setLastError          = (uint)(attributes & PInvokeAttributes.SupportsLastError) > 0U;
            bool bestFitMapping        = (attributes & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (attributes & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (uint)(method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) > 0U;

            return((Attribute) new DllImportAttribute(importDll, importName, charSet, exactSpelling, setLastError, preserveSig, callingConvention, bestFitMapping, throwOnUnmappableChar));
        }