Beispiel #1
0
        public object Create(Type type)
        {
            if (type.IsClass && !type.IsInterface && type != typeof(string))
            {
                var constructorWithMostParams = GetConstructorWithMostParams(type);

                var constructorParams = new List <object>();

                foreach (var parameter in constructorWithMostParams.GetParameters())
                {
                    var parameterInstance = _objectBuilder.Create(parameter.ParameterType) ?? _parameterCreator.Create(parameter);

                    if (parameterInstance == null)
                    {
                        throw new NullReferenceException($"Can not create object for parameter Type: {parameter.ParameterType.Name} when instantiating object {type.FullName}");
                    }

                    constructorParams.Add(parameterInstance);
                }

                return(constructorWithMostParams.Invoke(constructorParams.ToArray()));
            }

            return(null);
        }
 public static T Create <T>(this IObjectBuilder builder) where T : class
 {
     return(builder.Create <T>(typeof(T)));
 }