Exemplo n.º 1
0
        public void ShouldCreateContainerType()
        {
            var assembly = _assemblyBuilder.CreateAssembly(Guid.NewGuid().ToString(), ModuleKind.Dll);
            var module   = assembly.MainModule;

            TypeReference baseType = module.Import(typeof(object));
            var           microContainerTypeRef = module.Import(typeof(IMicroContainer));

            TypeReference[] interfaces    = new TypeReference[] { microContainerTypeRef };
            string          typeName      = "Hiro.MicroContainer";
            string          namespaceName = "Hiro.Containers";

            var            typeBuilder = new ContainerTypeBuilder();
            TypeDefinition result      = typeBuilder.CreateType(typeName, namespaceName, baseType, assembly, interfaces);

            // Verify the type attributes
            Assert.IsTrue(result.IsAutoClass);
            Assert.IsTrue(result.IsClass);
            Assert.IsTrue(result.IsBeforeFieldInit);
            Assert.IsTrue(result.IsPublic);

            Assert.IsTrue(result.Implements(microContainerTypeRef));

            // Verify that the default constructor exists
            var constructor = result.GetDefaultConstructor();

            Assert.IsNotNull(constructor);
            Assert.IsTrue(constructor.Parameters.Count == 0);
        }
Exemplo n.º 2
0
        private void ShouldProvideMethodOverrideFor <T>(string methodName)
            where T : class
        {
            MethodInfo targetMethod = typeof(T).GetMethod(methodName);

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

            var            objectType  = module.Import(typeof(object));
            var            typeBuilder = new ContainerTypeBuilder();
            TypeDefinition hostType    = typeBuilder.CreateType("Test", "Test", objectType, assembly);

            var overrider = new MethodOverrider();

            overrider.AddOverrideFor(targetMethod, hostType);

            // Search for the target method and make sure it matches the target method
            // signature
            var newMethod = (from MethodDefinition method in hostType.Methods
                             where method.Name == methodName
                             select method).First();

            // Match the parameters
            var index = 0;

            foreach (var param in targetMethod.GetParameters())
            {
                var parameterTypeRef = module.Import(param.ParameterType);
                var currentParameter = newMethod.Parameters[index];

                Assert.IsTrue(currentParameter.ParameterType.IsEquivalentTo(parameterTypeRef));
                index++;
            }

            // Match the return type
            var returnTypeRef = module.Import(targetMethod.ReturnType);

            if (!(returnTypeRef is GenericInstanceType))
            {
                Assert.IsTrue(newMethod.ReturnType.IsEquivalentTo(returnTypeRef));
            }
            else
            {
                var first  = (GenericInstanceType)returnTypeRef;
                var second = (GenericInstanceType)newMethod.ReturnType;

                Assert.IsTrue(first.ElementType.IsEquivalentTo(second.ElementType));
            }

            // Verify the method attributes
            Assert.IsTrue(newMethod.IsVirtual);
            Assert.AreEqual(newMethod.IsPublic, targetMethod.IsPublic);
            Assert.AreEqual(newMethod.IsStatic, targetMethod.IsStatic);
            Assert.AreEqual(newMethod.IsHideBySig, targetMethod.IsHideBySig);
        }
Exemplo n.º 3
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));
            }
        }