示例#1
0
        // Define a PInvoke method for this class.
        public MethodBuilder DefinePInvokeMethod
            (String name, String dllName, String entryName,
            MethodAttributes attributes,
            CallingConventions callingConvention,
            Type returnType, Type[] parameterTypes,
            CallingConvention nativeCallConv,
            CharSet nativeCharSet)
        {
            try
            {
                // Lock down the assembly while we do this.
                StartSync();

                // Validate the parameters.
                if (name == null)
                {
                    throw new ArgumentNullException("name");
                }
                if (name == String.Empty)
                {
                    throw new ArgumentException(_("Emit_NameEmpty"));
                }
                if (dllName == null)
                {
                    throw new ArgumentNullException("dllName");
                }
                if (dllName == String.Empty)
                {
                    throw new ArgumentException(_("Emit_NameEmpty"));
                }
                if (entryName == null)
                {
                    throw new ArgumentNullException("entryName");
                }
                if (entryName == String.Empty)
                {
                    throw new ArgumentException(_("Emit_NameEmpty"));
                }
                if ((type.Attributes & TypeAttributes.ClassSemanticsMask)
                    == TypeAttributes.Interface)
                {
                    throw new ArgumentException
                              (_("Emit_PInvokeInInterface"));
                }
                if ((attributes & MethodAttributes.Abstract) != 0)
                {
                    throw new ArgumentException
                              (_("Emit_PInvokeAbstract"));
                }

                // Create the underlying method.
                MethodBuilder method = new MethodBuilder
                                           (this, name,
                                           attributes | MethodAttributes.PinvokeImpl,
                                           callingConvention, returnType, parameterTypes);

                // Build the attributes for the PInvoke declaration.
                int pinvAttrs = (((int)nativeCallConv) << 8);
                switch (nativeCharSet)
                {
                case CharSet.Ansi:              pinvAttrs |= 0x0002; break;

                case CharSet.Unicode:   pinvAttrs |= 0x0004; break;

                case CharSet.Auto:              pinvAttrs |= 0x0006; break;

                default:                                break;
                }

                // Create the PInvoke declaration on the method.
                if (entryName == name)
                {
                    entryName = null;
                }
                lock (typeof(AssemblyBuilder))
                {
                    MethodBuilder.ClrMethodAddPInvoke
                        (((IClrProgramItem)method).ClrHandle,
                        pinvAttrs, dllName, entryName);
                }

                // Return the method to the caller.
                return(method);
            }
            finally
            {
                EndSync();
            }
        }