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); }
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); }
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)); } }
protected override void Seed(LocationsDBContext context) { var coordinates = new List <GPSLocation> { new GPSLocation(20, 54), new GPSLocation(23, 54), new GPSLocation(25, 54), new GPSLocation(27, 54), new GPSLocation(35, 54), new GPSLocation(42, 54), new GPSLocation(46, 60), new GPSLocation(48, 65), new GPSLocation(50, 65), new GPSLocation(58, 65), new GPSLocation(2, 30), new GPSLocation(4, 34), new GPSLocation(6, 34), new GPSLocation(12, 80), new GPSLocation(18, 80), new GPSLocation(25, 80), new GPSLocation(25, 84), new GPSLocation(25, 90), new GPSLocation(25, 99), }; if (!context.GPSLocation.Any()) { context.GPSLocation.AddRange(coordinates); } var containerTypeOrange = ContainerTypeBuilder.Create() .ColouredOrange() .WithDescription(ContainerTypeDescription.Orange.ToString()) .Build(); var containerTypeYellow = ContainerTypeBuilder.Create() .ColouredYellow() .WithDescription(ContainerTypeDescription.Yellow.ToString()) .Build(); var containerTypeBrown = ContainerTypeBuilder.Create() .ColouredBrown() .WithDescription(ContainerTypeDescription.Brown.ToString()) .Build(); var containerTypeGreen = ContainerTypeBuilder.Create() .ColouredGreen() .WithDescription(ContainerTypeDescription.Green.ToString()) .Build(); var containerTypeBlue = ContainerTypeBuilder.Create() .ColouredBlue() .WithDescription(ContainerTypeDescription.Blue.ToString()) .Build(); var containerType = new List <ContainerType> { containerTypeOrange, containerTypeYellow, containerTypeBrown, containerTypeBlue, containerTypeGreen, }; if (!context.ContainerType.Any()) { context.ContainerType.AddRange(containerType); } var pickuplocations = new List <PickUpLocation> { PickUpLocation.Create(coordinates[0], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[0], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[0], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[1], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[1], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), false), PickUpLocation.Create(coordinates[1], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), true), PickUpLocation.Create(coordinates[2], containerType.SingleOrDefault(ct => ct.Id == containerTypeGreen.Id), true), PickUpLocation.Create(coordinates[2], containerType.SingleOrDefault(ct => ct.Id == containerTypeBlue.Id), false), PickUpLocation.Create(coordinates[3], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[3], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[3], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), true), PickUpLocation.Create(coordinates[4], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), false), PickUpLocation.Create(coordinates[4], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[4], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[5], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[6], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[6], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[6], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[7], containerType.SingleOrDefault(ct => ct.Id == containerTypeGreen.Id), false), PickUpLocation.Create(coordinates[7], containerType.SingleOrDefault(ct => ct.Id == containerTypeBlue.Id), false), PickUpLocation.Create(coordinates[8], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[9], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[9], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[9], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[10], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[10], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), false), PickUpLocation.Create(coordinates[10], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[11], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), false), PickUpLocation.Create(coordinates[11], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[11], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), true), PickUpLocation.Create(coordinates[12], containerType.SingleOrDefault(ct => ct.Id == containerTypeGreen.Id), true), PickUpLocation.Create(coordinates[12], containerType.SingleOrDefault(ct => ct.Id == containerTypeBlue.Id), true), PickUpLocation.Create(coordinates[13], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[14], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[14], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[14], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), true), PickUpLocation.Create(coordinates[15], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[15], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), false), PickUpLocation.Create(coordinates[15], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[16], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), true), PickUpLocation.Create(coordinates[16], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), true), PickUpLocation.Create(coordinates[16], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), true), PickUpLocation.Create(coordinates[17], containerType.SingleOrDefault(ct => ct.Id == containerTypeOrange.Id), false), PickUpLocation.Create(coordinates[17], containerType.SingleOrDefault(ct => ct.Id == containerTypeYellow.Id), false), PickUpLocation.Create(coordinates[17], containerType.SingleOrDefault(ct => ct.Id == containerTypeBrown.Id), false), PickUpLocation.Create(coordinates[18], containerType.SingleOrDefault(ct => ct.Id == containerTypeGreen.Id), true), PickUpLocation.Create(coordinates[18], containerType.SingleOrDefault(ct => ct.Id == containerTypeBlue.Id), true), }; if (!context.PickUpLocation.Any()) { context.PickUpLocation.AddRange(pickuplocations); } context.SaveChanges(); }