コード例 #1
0
        private IDynamicTypeBuilder CreateDynamicTypeBuilder()
        {
            var ctorParams = new Dictionary <string, Type>(ctorExtraParamsType)
            {
                { "contractedService", targetType }
            };

            switch (typeCategory)
            {
            case TypeCategories.Class:
                return(DynamicTypeBuilderFactory.CreateClassBuilder($"{targetType.Name}{namePostfix}", ctorParams, moduleBuilder));

            case TypeCategories.Dto:
                return(DynamicTypeBuilderFactory.CreateDtoBuilder($"{targetType.Name}{namePostfix}", moduleBuilder));

            case TypeCategories.Interface:
                return(DynamicTypeBuilderFactory.CreateInterfaceBuilder($"{targetType.Name}{namePostfix}", moduleBuilder, interfaces));

            case TypeCategories.Implementation:
                return(DynamicTypeBuilderFactory.CreateClassBuilder($"{targetType.Name}{namePostfix}", ctorParams, moduleBuilder, interfaces));

            default:
                throw new Exception();
            }
        }
        public void Dynamic_Interface_Must_Be_Generated_With_Its_Methods()
        {
            var method1           = "Method1";
            var method1ReturnType = typeof(string);
            var method1Params     = new[] { typeof(int), typeof(string), typeof(Guid) };

            var method2           = "Method2";
            var method2ReturnType = typeof(void);
            var method2Params     = new[] { typeof(object), typeof(Guid), typeof(long) };

            var interfaceFullName = "Dynamic.GeneratedInterface";

            var interfaceBuilder = DynamicTypeBuilderFactory.CreateInterfaceBuilder(interfaceFullName);

            DefineMethod(method1, method1ReturnType, method1Params, interfaceBuilder);
            DefineMethod(method2, method2ReturnType, method2Params, interfaceBuilder);

            var interfaceType = interfaceBuilder.Build();

            AssertOnHavingMethod(
                type: interfaceType,
                methodName: method1,
                returnType: method1ReturnType,
                paramTypes: method1Params);

            AssertOnHavingMethod(
                type: interfaceType,
                methodName: method2,
                returnType: method2ReturnType,
                paramTypes: method2Params);
        }
コード例 #3
0
        public void Dynamic_Class_Must_Be_Created_With_Specified_Method_With_Its_Parameter_Names()
        {
            var method1Name       = "Method1";
            var method1ReturnType = typeof(string);
            var method1Params     = new Dictionary <Type, string>
            {
                { typeof(int), "number" },
                { typeof(string), "message" },
            };
            var attributeType      = typeof(SampleAttribute);
            var ctorParamsMapping  = SampleAttribute.GetCtorParamValueMapping();
            var propsValuesMapping = SampleAttribute.GetPropertyValueMaaping();
            var setAttributeParam  = new Dictionary <Type, Tuple <IDictionary <Type, object>, IDictionary <string, object> > >
            {
                { attributeType, new Tuple <IDictionary <Type, object>, IDictionary <string, object> >(ctorParamsMapping, propsValuesMapping) },
            };


            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", new Dictionary <string, Type>());

            SetMethod(builder, method1Name, method1ReturnType, method1Params, setAttributeParam);

            var classType = builder.Build();

            AssertOnHavingMethodWithParamNames(
                classType: classType,
                methodName: method1Name,
                returnType: method1ReturnType,
                @params: method1Params,
                propValuesMapping: new Dictionary <Type, IDictionary <string, object> >
            {
                { attributeType, propsValuesMapping },
            });
        }
コード例 #4
0
        private static object CreateCommandServiceObject(
            CommandsInvokationEvaluator commandInvokationEvaluator,
            Type serviceType,
            IOptimizationPackage optPack)
        {
            var serviceObjectType = default(Type);

            if (optPack.typeContainer.Contains($"{serviceType}_Dispatcher"))
            {
                serviceObjectType = optPack.typeContainer.Get($"{serviceType}_Dispatcher");
            }
            else
            {
                var moduleBuilder = ModuleBuilderProvider.GetModuleBuilder();

                var serviceObjectTypeBuilder = DynamicTypeBuilderFactory.CreateClassBuilder(
                    $"{serviceType}_Dispatcher",
                    new Dictionary <string, Type>(),
                    moduleBuilder,
                    serviceType, typeof(IDisposable));

                serviceObjectType = serviceObjectTypeBuilder.Build();

                optPack.typeContainer.Save(serviceObjectType);
            }

            var serviceObject = Activator.CreateInstance(serviceObjectType, commandInvokationEvaluator);

            return(serviceObject);
        }
コード例 #5
0
        public void Dynamic_Class_Must_Be_Created_With_Defined_Methods()
        {
            var method1Name       = "Method1";
            var method1ReturnType = typeof(string);
            var method1Params     = new List <Type> {
                typeof(int), typeof(string)
            };

            var method2Name       = "Method2";
            var method2ReturnType = typeof(void);
            var method2Params     = new List <Type> {
                typeof(object)
            };

            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", new Dictionary <string, Type>());

            SetMethod(builder, method1Name, method1ReturnType, method1Params);
            SetMethod(builder, method2Name, method2ReturnType, method2Params);

            var classType = builder.Build();

            AssertOnHavingMethod(
                classType: classType,
                methodName: method1Name,
                returnType: method1ReturnType,
                @params: method1Params);

            AssertOnHavingMethod(
                classType: classType,
                methodName: method2Name,
                returnType: method2ReturnType,
                @params: method2Params);
        }
コード例 #6
0
        private static Type GenerateDynamicDtoType(
            string typeName,
            IDictionary <string, Type> propertiesNameTypeMapping)
        {
            var typeBuilder = DynamicTypeBuilderFactory.CreateDtoBuilder(typeName);

            foreach (var propertyName in propertiesNameTypeMapping.Keys)
            {
                typeBuilder.SetProperty(propertyName, propertiesNameTypeMapping[propertyName]);
            }

            var generatedType = typeBuilder.Build();

            return(generatedType);
        }
コード例 #7
0
        public void Only_One_Assembly_Must_Be_Loaded()
        {
            var asmName = new AssemblyName("SomeName");

            var asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndCollect);

            var moduleBuilder = asmBuilder.DefineDynamicModule("SomeModule");

            var builder1 = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass1", new Dictionary <string, Type>(), moduleBuilder);
            var builder2 = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass2", new Dictionary <string, Type>(), moduleBuilder);

            var classType1 = builder1.Build();
            var classType2 = builder2.Build();

            Assert.Equal(classType2.Assembly, classType1.Assembly);
        }
        public void Dynamic_Interface_Must_Be_Generated_With_Defined_Attribute()
        {
            var interfaceFullName = "Dynamic.GeneratedInterface";
            var attributeType     = typeof(SampleAttribute);
            var attributeCtorParamValueMapping = SampleAttribute.GetCtorParamValueMapping();
            var attributePropertyValueMapping  = SampleAttribute.GetPropertyValueMaaping();

            var interfaceBuilder = DynamicTypeBuilderFactory.CreateInterfaceBuilder(interfaceFullName);

            interfaceBuilder.SetAttribute(attributeType, attributeCtorParamValueMapping, attributePropertyValueMapping);

            var generatedType = interfaceBuilder.Build();

            AssertOnHavingAttributeOnType(
                interfaceType: generatedType,
                attributeType: attributeType,
                attributeCtorParamValueMapping: attributeCtorParamValueMapping,
                attributePropertyValueMapping: attributePropertyValueMapping);
        }
        public void Dynamic_Generated_Interface_Must_Implemented_Specified_Interfaces()
        {
            var @interface  = typeof(ISampleInterface);
            var @interface2 = typeof(ISampleInterface2);

            var classBuilder =
                DynamicTypeBuilderFactory.CreateInterfaceBuilder(
                    "DynamicInterface", null,
                    @interface, @interface2);

            var type = classBuilder.Build();

            AssertOnTypeHasImplementededInterface(
                type: type,
                implementedInterface: @interface);

            AssertOnTypeHasImplementededInterface(
                type: type,
                implementedInterface: @interface2);
        }
コード例 #10
0
        public void Dynamic_Dto_Type_Must_Have_The_Specified_Attribute_On_The_Defined_Property()
        {
            var propertyName  = "SomeProperty";
            var attributeType = typeof(SampleAttribute);
            var ctorParametersValuesMapping = SampleAttribute.GetCtorParamValueMapping();
            var propertiesValuesMapping     = SampleAttribute.GetPropertyValueMaaping();

            var typeBuilder = DynamicTypeBuilderFactory.CreateDtoBuilder(propertyName);

            typeBuilder
            .SetProperty(propertyName, typeof(string))
            .SetAttribute(
                attributeType,
                ctorParametersValuesMapping,
                propertiesValuesMapping);

            var generatedType = typeBuilder.Build();

            AssertOnHavingPropertyWithFollowingAttribute(generatedType, propertyName, typeof(SampleAttribute), propertiesValuesMapping);
        }
        public void Dynamic_Generated_Class_Must_Implemented_Specified_Interfaces()
        {
            var @interface  = typeof(ISampleInterface);
            var @interface2 = typeof(ISampleInterface2);

            var classBuilder =
                DynamicTypeBuilderFactory.CreateClassBuilder(
                    @interface.Name,
                    new Dictionary <string, Type>(), null,
                    @interface, @interface2);

            var type = classBuilder.Build();

            AssertOnTypeHasImplementededInterface(
                type: type,
                implementedInterface: @interface);

            AssertOnTypeHasImplementededInterface(
                type: type,
                implementedInterface: @interface2);
        }
コード例 #12
0
        public void Dynamic_Class_Must_Have_The_Ctor_With_Specified_Params_Plus_Invokaction_Evaluator()
        {
            var ctorTypeMapping = new Dictionary <string, Type>
            {
                { "arg1", typeof(string) },
                { "arg2", typeof(int) },
                { "arg3", typeof(Guid) },
            };

            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", ctorTypeMapping);

            var classType = builder.Build();

            var allCtorParams = new List <Type> {
                typeof(IInvokationEvaluator)
            };

            allCtorParams.AddRange(ctorTypeMapping.Values);

            AssertOnHavingCtorWithSpecifiedArgs(classType, allCtorParams.ToArray());
        }
コード例 #13
0
        public void Dynamic_Class_Must_Be_Created_With_The_Defined_Working_Method_With_Specified_Return_Type(Type returnType)
        {
            var method1Name       = "Method1";
            var method1ReturnType = returnType;
            var method1Params     = new List <Type> {
                typeof(int), typeof(string), typeof(Exception)
            };

            var testEvaluator = new TestEvaluatorMock();

            var ctorParamValueMapping = new Dictionary <Type, object>
            {
                { typeof(IInvokationEvaluator), testEvaluator },
                { typeof(string), "someString" },
            };

            var paramsValueMapping = new Dictionary <Type, object>
            {
                { typeof(int), 21 },
                { typeof(string), "sdf" },
                { typeof(Exception), new Exception("Hello Evaluator") },
            };

            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", new Dictionary <string, Type> {
                { "someId", typeof(string) }
            });

            SetMethod(builder, method1Name, method1ReturnType, method1Params);

            var classType = builder.Build();

            CallMethodOfTypeWithParams(
                method1Name,
                classType,
                ctorParamValueMapping,
                paramsValueMapping);

            testEvaluator.AssertInvokationContext();
        }
コード例 #14
0
        public void Dynamic_Class_Must_Instanciated_With_Specified_Field_Values()
        {
            var ctorTypeMapping = new Dictionary <string, Type>
            {
                { "arg1", typeof(string) },
                { "arg2", typeof(int) },
                { "arg3", typeof(Guid) },
            };

            var fieldValues = new Dictionary <string, object>
            {
                { "_evaluator", new TestEvaluatorMock() },
                { "_arg1", "SampleString" },
                { "_arg2", 456 },
                { "_arg3", Guid.NewGuid() },
            };

            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", ctorTypeMapping);

            var classType = builder.Build();

            AssertInstaciatedObjectHavingSpecifiedFieldValues(classType, fieldValues);
        }