Esempio n. 1
0
        private object CreateUsingConstructor(Type type)
        {
            if (type.GetConstructor(Type.EmptyTypes) != null)
            {
                return(_typeActivator.ActivateInstance(type));
            }

            foreach (var constructor in type.GetConstructors().OrderBy(x => x.GetParameters().Count()))
            {
                List <object> parametersInstances = new List <object>();

                foreach (var parameter in constructor.GetParameters())
                {
                    var parameterInstance = CreateInstance(parameter.ParameterType);
                    if (parameterInstance == null)
                    {
                        parametersInstances = null;
                        break;
                    }
                    parametersInstances.Add(parameterInstance);
                }

                if (parametersInstances != null)
                {
                    return(_typeActivator.ActivateInstance(type, parametersInstances.ToArray()));
                }
            }

            return(null);
        }