Beispiel #1
0
        private void TestCreatePublicMethod(bool isStatic)
        {
            var typeBuilder              = new ContainerTypeBuilder();
            var methodBuilder            = new MethodBuilder();
            MethodBuilderOptions options = new MethodBuilderOptions();

            var assembly = _assemblyBuilder.CreateAssembly("SomeAssembly", ModuleKind.Dll);
            var module   = assembly.MainModule;

            var objectType = module.Import(typeof(object));
            var targetType = typeBuilder.CreateType("SomeType", "SomeNamespace", objectType, assembly);

            var methodName = "SomeMethod";

            options.MethodName = methodName;
            options.HostType   = targetType;
            options.SetMethodParameters(typeof(int), typeof(int));
            options.ReturnType = typeof(void);

            options.IsPublic = true;
            options.IsStatic = isStatic;

            MethodDefinition result = methodBuilder.CreateMethod(options);

            // Verify the method attributes
            Assert.IsTrue(result.IsPublic);
            Assert.IsFalse(result.IsAbstract);
            if (!isStatic)
            {
                Assert.IsTrue(result.HasThis);
                Assert.IsTrue(result.IsVirtual);
            }
            else
            {
                Assert.IsTrue(result.IsStatic);
            }

            // Check the method signature
            Assert.IsTrue(result.Parameters.Count == 2);

            var integerType = module.Import(typeof(int));

            foreach (ParameterDefinition param in result.Parameters)
            {
                Assert.IsTrue(param.ParameterType.IsEquivalentTo(integerType));
            }
        }