Ejemplo n.º 1
0
            public void CopyTo(DmdCustomAttributeData[] destination, ref int index, DmdMarshalType marshalType)
            {
                if (Count == 0)
                {
                    return;
                }
                int argsCount = 5;

                if (marshalType.MarshalType != null)
                {
                    argsCount++;
                }
                if ((object)marshalType.MarshalTypeRef != null)
                {
                    argsCount++;
                }
                if (marshalType.MarshalCookie != null)
                {
                    argsCount++;
                }
                if ((object)marshalType.SafeArrayUserDefinedSubType != null)
                {
                    argsCount++;
                }
                var type              = ctor.ReflectedType;
                var appDomain         = type.AppDomain;
                var unmanagedTypeType = appDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_UnmanagedType, isOptional: false);
                var varEnumType       = appDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_VarEnum, isOptional: false);
                var namedArgs         = new DmdCustomAttributeNamedArgument[argsCount];
                int w = 0;

                namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("ArraySubType"), new DmdCustomAttributeTypedArgument(unmanagedTypeType, (int)marshalType.ArraySubType));
                namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("SizeParamIndex"), new DmdCustomAttributeTypedArgument(appDomain.System_Int16, marshalType.SizeParamIndex));
                namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("SizeConst"), new DmdCustomAttributeTypedArgument(appDomain.System_Int32, marshalType.SizeConst));
                namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("IidParameterIndex"), new DmdCustomAttributeTypedArgument(appDomain.System_Int32, marshalType.IidParameterIndex));
                namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("SafeArraySubType"), new DmdCustomAttributeTypedArgument(varEnumType, (int)marshalType.SafeArraySubType));
                if (marshalType.MarshalType != null)
                {
                    namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("MarshalType"), new DmdCustomAttributeTypedArgument(appDomain.System_String, marshalType.MarshalType));
                }
                if ((object)marshalType.MarshalTypeRef != null)
                {
                    namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("MarshalTypeRef"), new DmdCustomAttributeTypedArgument(appDomain.System_Type, marshalType.MarshalTypeRef));
                }
                if (marshalType.MarshalCookie != null)
                {
                    namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("MarshalCookie"), new DmdCustomAttributeTypedArgument(appDomain.System_String, marshalType.MarshalCookie));
                }
                if ((object)marshalType.SafeArrayUserDefinedSubType != null)
                {
                    namedArgs[w++] = new DmdCustomAttributeNamedArgument(type.GetField("SafeArrayUserDefinedSubType"), new DmdCustomAttributeTypedArgument(appDomain.System_Type, marshalType.SafeArrayUserDefinedSubType));
                }
                if (namedArgs.Length != w)
                {
                    throw new InvalidOperationException();
                }
                var ctorArgs = new[] { new DmdCustomAttributeTypedArgument(unmanagedTypeType, (int)marshalType.Value) };

                destination[index++] = new DmdCustomAttributeData(ctor, ctorArgs, namedArgs, isPseudoCustomAttribute: true);
            }
Ejemplo n.º 2
0
            public void CopyTo(DmdCustomAttributeData[] destination, ref int index, DmdMethodInfo method, ref DmdImplMap?implMap)
            {
                if (Count == 0)
                {
                    return;
                }

                var appDomain             = ctor.AppDomain;
                var im                    = implMap.Value;
                var attributes            = im.Attributes;
                var charSetType           = appDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_CharSet, isOptional: false);
                var callingConventionType = appDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_CallingConvention, isOptional: false);

                CharSet charSet;

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

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

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

                case DmdPInvokeAttributes.CharSetAuto:          charSet = CharSet.Auto; break;
                }

                CallingConvention callingConvention;

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

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

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

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

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

                default:                                                                                callingConvention = CallingConvention.Cdecl; break;
                }

                var ctorArgs  = new[] { new DmdCustomAttributeTypedArgument(appDomain.System_String, im.Module) };
                var type      = ctor.ReflectedType;
                var namedArgs = new DmdCustomAttributeNamedArgument[8] {
                    new DmdCustomAttributeNamedArgument(type.GetField("EntryPoint"), new DmdCustomAttributeTypedArgument(appDomain.System_String, im.Name)),
                    new DmdCustomAttributeNamedArgument(type.GetField("CharSet"), new DmdCustomAttributeTypedArgument(charSetType, (int)charSet)),
                    new DmdCustomAttributeNamedArgument(type.GetField("ExactSpelling"), new DmdCustomAttributeTypedArgument(appDomain.System_Boolean, (attributes & DmdPInvokeAttributes.NoMangle) != 0)),
                    new DmdCustomAttributeNamedArgument(type.GetField("SetLastError"), new DmdCustomAttributeTypedArgument(appDomain.System_Boolean, (attributes & DmdPInvokeAttributes.SupportsLastError) != 0)),
                    new DmdCustomAttributeNamedArgument(type.GetField("PreserveSig"), new DmdCustomAttributeTypedArgument(appDomain.System_Boolean, method.IsPreserveSig)),
                    new DmdCustomAttributeNamedArgument(type.GetField("CallingConvention"), new DmdCustomAttributeTypedArgument(callingConventionType, (int)callingConvention)),
                    new DmdCustomAttributeNamedArgument(type.GetField("BestFitMapping"), new DmdCustomAttributeTypedArgument(appDomain.System_Boolean, (attributes & DmdPInvokeAttributes.BestFitMask) == DmdPInvokeAttributes.BestFitEnabled)),
                    new DmdCustomAttributeNamedArgument(type.GetField("ThrowOnUnmappableChar"), new DmdCustomAttributeTypedArgument(appDomain.System_Boolean, (attributes & DmdPInvokeAttributes.ThrowOnUnmappableCharMask) == DmdPInvokeAttributes.ThrowOnUnmappableCharEnabled)),
                };

                destination[index++] = new DmdCustomAttributeData(ctor, ctorArgs, namedArgs, isPseudoCustomAttribute: true);
            }