public static void TestDefinePInvokeMethod3(DpmParams p)
        {
            TypeBuilder   tb = Helpers.DynamicType(TypeAttributes.Public);
            MethodBuilder mb = tb.DefinePInvokeMethod(
                p.MethodName,
                p.LibName,
                p.EntrypointName,
                p.Attributes,
                p.ManagedCallConv,
                p.ReturnType,
                p.ReturnTypeReqMods,
                p.ReturnTypeOptMods,
                p.ParameterTypes,
                p.ParameterTypeReqMods,
                p.ParameterTypeOptMods,
                p.NativeCallConv,
                p.Charset);

            mb.SetImplementationFlags(mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

            Type       t = tb.CreateType();
            MethodInfo m = t.GetMethod(p.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

            Assert.NotNull(m);
            VerifyPInvokeMethod(t, m, p);
        }
        public static void TestDefinePInvokeMethod2(DpmParams p)
        {
            ModuleBuilder modb = Helpers.DynamicModule();
            MethodBuilder mb   = modb.DefinePInvokeMethod(
                p.MethodName,
                p.LibName,
                p.Attributes,
                p.ManagedCallConv,
                p.ReturnType,
                p.ParameterTypes,
                p.NativeCallConv,
                p.Charset);

            mb.SetImplementationFlags(mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

            modb.CreateGlobalFunctions();
            MethodInfo m = modb.GetMethod(p.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, CallingConventions.Any, p.ParameterTypes, null);

            Assert.NotNull(m);
            TypeBuilderDefinePInvokeMethodTests.VerifyPInvokeMethod(m.DeclaringType, m, p);
        }
        internal static void VerifyPInvokeMethod(Type type, MethodInfo method, DpmParams p)
        {
            Assert.Equal(type.AsType(), method.DeclaringType);
            Assert.Equal(p.MethodName, method.Name);
            Assert.Equal(p.Attributes, method.Attributes);
            Assert.Equal(p.ManagedCallConv, method.CallingConvention);
            Assert.Equal(p.ReturnType, method.ReturnType);

            ParameterInfo[] parameters = method.GetParameters();
            Assert.Equal <Type>(p.ParameterTypes, parameters.Select(pi => pi.ParameterType));

            DllImportAttribute dia = method.GetCustomAttribute <DllImportAttribute>();

            Assert.NotNull(dia);
            Assert.Equal(p.LibName, dia.Value);
            Assert.Equal(p.EntrypointName, dia.EntryPoint);
            Assert.Equal(p.Charset, dia.CharSet);
            Assert.Equal(p.NativeCallConv, dia.CallingConvention);
            Assert.Equal(false, dia.BestFitMapping);
            Assert.Equal(false, dia.ExactSpelling);
            Assert.Equal(true, dia.PreserveSig);
            Assert.Equal(false, dia.SetLastError);

            IList <Type> returnTypeOptMods = method.ReturnParameter.GetOptionalCustomModifiers();

            if (p.ReturnTypeOptMods == null)
            {
                Assert.Equal(0, returnTypeOptMods.Count);
            }
            else
            {
                Assert.Equal <Type>(p.ReturnTypeOptMods, returnTypeOptMods);
            }

            IList <Type> returnTypeReqMods = method.ReturnParameter.GetRequiredCustomModifiers();

            if (p.ReturnTypeReqMods == null)
            {
                Assert.Equal(0, returnTypeReqMods.Count);
            }
            else
            {
                Assert.Equal <Type>(p.ReturnTypeReqMods, returnTypeReqMods);
            }

            if (p.ParameterTypeOptMods == null)
            {
                foreach (ParameterInfo pi in method.GetParameters())
                {
                    Assert.Equal(0, pi.GetOptionalCustomModifiers().Length);
                }
            }
            else
            {
                Assert.Equal(parameters.Length, p.ParameterTypeOptMods.Length);
                for (int i = 0; i < p.ParameterTypeOptMods.Length; i++)
                {
                    Type[] mods = parameters[i].GetOptionalCustomModifiers();
                    Assert.Equal <Type>(p.ParameterTypeOptMods[i], mods);
                }
            }

            if (p.ParameterTypeReqMods == null)
            {
                foreach (ParameterInfo pi in method.GetParameters())
                {
                    Assert.Equal(0, pi.GetRequiredCustomModifiers().Length);
                }
            }
            else
            {
                Assert.Equal(parameters.Length, p.ParameterTypeReqMods.Length);
                for (int i = 0; i < p.ParameterTypeReqMods.Length; i++)
                {
                    Type[] mods = parameters[i].GetRequiredCustomModifiers();
                    Assert.Equal <Type>(p.ParameterTypeReqMods[i], mods);
                }
            }
        }