Example #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);
        }
        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);
        }