public T Construct(int index) { bool requiresArgs = reflectionUtil.RequiresConstructorArgs(typeof(T)); if (typeof(T).IsInterface) { throw new TypeCreationException("Cannot build an interface"); } if (typeof(T).IsAbstract) { throw new TypeCreationException("Cannot build an abstract class"); } T obj; if (_constructorExpression != null) { obj = _constructorExpression.Compile().Invoke(index); } else if (requiresArgs && constructorArgs != null) { obj = reflectionUtil.CreateInstanceOf <T>(constructorArgs); } else if (constructorArgs != null) { obj = reflectionUtil.CreateInstanceOf <T>(constructorArgs); } else { obj = reflectionUtil.CreateInstanceOf <T>(); } return(obj); }
public void ShouldBeAbleToBuildAList() { IDeclaration <MyClass> declaration = Substitute.For <IDeclaration <MyClass> >(); var builder = new ListBuilder <MyClass>(listSize, propertyNamer, reflectionUtil, new BuilderSettings()); reflectionUtil.RequiresConstructorArgs(typeof(MyClass)).Returns(false); reflectionUtil.CreateInstanceOf <MyClass>().Returns(myClass); declaration.Construct(); declaration.AddToMaster(Arg.Any <MyClass[]>()); propertyNamer.SetValuesOfAllIn(Arg.Any <IList <MyClass> >()); declaration.CallFunctions(Arg.Any <IList <MyClass> >()); builder.Build(); }
public void ShouldBeAbleToConstructAnObjectWithNoConstructorArgs() { reflectionUtil.RequiresConstructorArgs(typeof(MyClass)).Returns(false); reflectionUtil.CreateInstanceOf <MyClass>().Returns(new MyClass()); builder.Construct(index: 1); }
public T Construct(int index) { var requiresArgs = reflectionUtil.RequiresConstructorArgs(typeof(T)); if (typeof(T).IsInterface()) { throw new TypeCreationException("Cannot build an interface"); } if (typeof(T).IsAbstract()) { throw new TypeCreationException("Cannot build an abstract class"); } if (_factoryFunction != null) { return(_factoryFunction.Invoke()); } if (_indexedFactoryFunction != null) { return(_indexedFactoryFunction.Invoke(index)); } //if (_indexedConstructorExpression != null) //{ // return _indexedConstructorExpression.Compile().Invoke(index); //} //if (_constructorExpression != null) //{ // return _constructorExpression.Compile().Invoke(); //} //if (requiresArgs && constructorArgs != null) //{ // return reflectionUtil.CreateInstanceOf<T>(constructorArgs); //} //if (constructorArgs != null) //{ // return reflectionUtil.CreateInstanceOf<T>(constructorArgs); //} return(reflectionUtil.CreateInstanceOf <T>()); }
public void ShouldBeAbleToCreateInstanceOfClass() { var instance = reflectionUtil.CreateInstanceOf <MyClass>(); instance.ShouldBeOfType <MyClass>(); }
public void ShouldBeAbleToCreateInstanceOfClass() { var instance = reflectionUtil.CreateInstanceOf <MyClass>(); Assert.That(instance, Is.TypeOf(typeof(MyClass))); }