示例#1
0
        public virtual T Build <T>(string variation = "", Action <T> userSpecifiedProperties = null) where T : class
        {
            var constructedObject = _constructorHelper.CreateInstance <T>(_costructors.Get <T>(variation));

            if (_properties.ContainsKey <T>(variation))
            {
                _propertyHelper.SetProperties(_properties.Get <T>(variation), constructedObject);
            }

            if (userSpecifiedProperties != null)
            {
                userSpecifiedProperties.Invoke(constructedObject);
            }

            // We should check if for the object properties we have a creation strategy and call create on that one.
            // Also if the property has a value, don't override.
            //foreach (PropertyInfo prop in constructedObject.GetType().GetProperties())
            //{
            //    if (!_costructors.ContainsKey(variation, prop.PropertyType) || prop.GetValue(constructedObject, null) != null)
            //        continue;

            //    // check if property has a setter
            //    if (prop.GetSetMethod() == null)
            //        continue;

            //    object value = GetType().
            //        GetMethod("CreateForChild").
            //        MakeGenericMethod(prop.PropertyType).
            //        Invoke(this, null);

            //    prop.SetValue(constructedObject, value, null);
            //}

            return(constructedObject);
        }
示例#2
0
        protected override ConstructorInfo Create(TypeInfo parameter)
        {
            var constructors = _constructors.Get(parameter).ToImmutableArray();
            var length       = constructors.Length;

            for (var i = 0; i < length; i++)
            {
                var constructor = constructors[i];
                if (_specification.IsSatisfiedBy(constructor))
                {
                    return(constructor);
                }
            }
            return(null);
        }
示例#3
0
 public IEnumerable <ConstructorInfo> Get(TypeInfo parameter)
 => parameter.DeclaredConstructors.Union(_constructors.Get(parameter));