private ObjectFactoryConstructionSpecification GetCustomObjectConstructionSpecificationOrNull(Type t) { ObjectFactoryConstructionSpecification typeConstructionSpecification = this.typeConstructionSpecifications.FirstOrDefault(spec => spec.Type.Equals(t)); return(typeConstructionSpecification); }
public object CreateInstance(Type type) { ObjectFactoryConstructionSpecification specification = this.GetCustomObjectConstructionSpecificationOrNull(type); if (specification != null) { return(specification.Constructor()); } else { IEnumerable <ConstructorInfo> constructorInfoCollection = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); ConstructorInfo constructorInfo = constructorInfoCollection.FirstOrDefault(constructor => !constructor.GetParameters().Any()) ?? constructorInfoCollection.First(); //Create a collection of default values IEnumerable <Type> constructorTypes = constructorInfo.GetParameters().Select(info => info.ParameterType); IEnumerable <object> defaultArguments = constructorTypes.Select(this.GenerateNew); object objectUnderTest = constructorInfo.Invoke(defaultArguments.ToArray()); return(objectUnderTest); } }