Пример #1
0
        private static Func <object> CreateTypeFactoryMethod(Type type)
        {
            var typeToCreate = type.IsInterface ? StateTypeEmitter.EmitType(type, new OwnedStateClassEmitter()) : type;

            if (!typeToCreate.IsValueType)
            {
                var ctor = typeToCreate.GetConstructor(Type.EmptyTypes);

                if (ctor == null || !ctor.IsPublic)
                {
                    throw new ArgumentException("Requested type has to have a default ctor", nameof(type));
                }

                DynamicMethod ctorCall = new DynamicMethod("GetInstanceOf",
                                                           MethodAttributes.Public | MethodAttributes.Static,
                                                           CallingConventions.Standard,
                                                           typeToCreate,
                                                           null,
                                                           typeof(EmittedTypeFactory),
                                                           true);

                var il = ctorCall.GetILGenerator(8);
                il.Emit(OpCodes.Newobj, ctor);
                il.Emit(OpCodes.Ret);

                return((Func <object>)ctorCall.CreateDelegate(typeof(Func <object>)));
            }
            else
            {
                var value = Activator.CreateInstance(typeToCreate);

                //Value types will be copied
                return(() => value);
            }
        }
        public void EmitType_OfInterfaceWithOnlyProperties_ShouldReturnTypeOfClass(object emitter)
        {
            //Arrange

            //Act
            var myType = StateTypeEmitter.EmitType(typeof(MyInterface), emitter as BaseClassEmitter);

            //Assert
            myType.IsClass.Should().BeTrue();
        }
        public void EmitType_OfInterfaceWithOnlyProperties_ShouldReturnTypeThatImplementsICanSwitchBackAndToReadOnly(object emitter)
        {
            //Arrange
            var myType = StateTypeEmitter.EmitType(typeof(MyInterface), emitter as BaseClassEmitter);

            //Act
            bool implementsMyInterface = myType.GetInterfaces().Any(x => x.Name == typeof(ICanSwitchBackAndToReadOnly).Name);

            //Assert
            implementsMyInterface.Should().BeTrue();
        }
Пример #4
0
        private static Func <object> CreateTypeFactoryMethod(Type type)
        {
            var typeToCreate = type.IsInterface ? StateTypeEmitter.EmitType(type, new OwnedStateClassEmitter()) : type;

            var ctor = typeToCreate.GetConstructor(Type.EmptyTypes);

            if (ctor == null || !ctor.IsPublic)
            {
                throw new ArgumentException("Requested type has to have a default ctor", nameof(type));
            }

            return(Expression.Lambda <Func <object> >(Expression.New(ctor)).Compile());
        }
Пример #5
0
        public void EmitType_OfDerivedInterface_ShouldSucceed()
        {
            //Arrange
            var  emitter = new OwnedStateClassEmitter();
            Type myType  = null;

            //Act
            var exception = Record.Exception((Action)(() => myType = StateTypeEmitter.EmitType(typeof(MyDerivedOfNameAndSalary), emitter)));

            //Assert
            exception.Should().BeNull();
            myType.Should().NotBeNull();
            myType.Should().Implement(typeof(MyDerivedOfNameAndSalary));
            myType.Should().Implement(typeof(MyBaseWithNameAndSalary));
        }
Пример #6
0
        public void EmitType_OfInterfaceDerivingFromTwoInterfaceWithSameNamedButDifferntTypeProperty_ShouldImplementPropertiesExplicitly()
        {
            //Arrange
            var  emitter = new OwnedStateClassEmitter();
            Type myType  = null;

            //Act
            var exception = Record.Exception((Action)(() => myType = StateTypeEmitter.EmitType(typeof(MyDerivedOfIntAndStringValues), emitter)));

            //Assert
            exception.Should().BeNull();
            myType.Should().NotBeNull();
            myType.Should().Implement(typeof(MyDerivedOfIntAndStringValues));
            myType.Should().Implement(typeof(MyBaseWithStringValue));
            myType.Should().Implement(typeof(MyBaseWithIntValue));
        }
Пример #7
0
        public void Setup()
        {
            var flagType        = typeof(IFlagForApplier);
            var openApplierType = typeof(TestBlankApplier <>);

            var appliers = new List <object>();

            for (int i = 0; i < Appliers; i++)
            {
                var stubClass = StateTypeEmitter.EmitType(flagType, new OwnedStateClassEmitter(), flagType.Name + i.ToString());
                var constructedApplierType = openApplierType.MakeGenericType(stubClass);
                appliers.Add(Activator.CreateInstance(constructedApplierType));
            }

            fixture = new AggregateFixture(Guid.NewGuid().ToString(), appliers.ToArray());

            for (int i = 0; i < Events; i++)
            {
                fixture.AddCinemaCreationEvent();
            }
        }
Пример #8
0
        private static WrapperCreationResult CreateWrapperFactoryMethor(Type type, bool throwException = true)
        {
            if (!type.IsInterface)
            {
                if (throwException)
                {
                    throw new TypeCannotBeWrappedException(type);
                }
                else
                {
                    return new WrapperCreationResult
                           {
                               Factory        = null,
                               FactoryCreated = false,
                           }
                };
            }

            var wrapperType = StateTypeEmitter.EmitType(type, new StateWrapperEmitter());

            var ctor = wrapperType.GetConstructor(new[] { type });

            if (ctor == null || !ctor.IsPublic)
            {
                throw new ArgumentException("Requested type has to have a default ctor", nameof(type));
            }

            var objectStateParam = ParameterExpression.Parameter(type, "wrapped");

            var ctorCall = Expression.New(ctor, objectStateParam);

            return(new WrapperCreationResult
            {
                Factory = Expression.Lambda(typeof(Func <,>).MakeGenericType(type, type),
                                            ctorCall, objectStateParam).Compile(),
                FactoryCreated = true,
            });
        }
    }