Exemple #1
0
        private object Resolve(ShittyRegistration registration)
        {
            if (registration == null)
            {
                return(null);
            }

            if (registration.ResolutionType == ResolutionType.Factory)
            {
                return(registration.Factory());
            }

            if (registration.Type.IsInterface)
            {
                return(null);
            }

            var resolvedType = registration.Type;
            var constructor  = resolvedType
                               .GetConstructors()
                               .OrderBy(x => x.GetParameters().Count())
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(string)))
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(bool)))
                               .LastOrDefault();

            if (constructor == null)
            {
                throw new EntryPointNotFoundException($"Unable to create an instance of {resolvedType.Name} as we cannot find a single public constructor for it. Make sure you have at least 1 public constructor (we'll pick the one with most arguments though)");
            }

            var parameters = constructor.GetParameters();

            object instance;

            if (!parameters.Any())
            {
                instance = Activator.CreateInstance(resolvedType);
            }
            else
            {
                instance = constructor.Invoke(
                    ResolveParameters(parameters).ToArray()
                    );
            }

            registration.AfterCreation?.Invoke(instance);

            return(instance);
        }
Exemple #2
0
        private object Resolve(ShittyRegistration registration)
        {
            if (registration == null)
            {
                return(null);
            }

            if (registration.ResolutionType == ResolutionType.Factory)
            {
                return(registration.Factory());
            }

            if (registration.Type.IsInterface)
            {
                return(null);
            }

            var resolvedType = registration.Type;
            var constructor  = resolvedType
                               .GetConstructors()
                               .OrderBy(x => x.GetParameters().Count())
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(string)))
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(bool)))
                               .LastOrDefault();
            var parameters = constructor.GetParameters();

            object instance;

            if (!parameters.Any())
            {
                instance = Activator.CreateInstance(resolvedType);
            }
            else
            {
                instance = constructor.Invoke(
                    ResolveParameters(parameters).ToArray()
                    );
            }

            if (registration.AfterCreation != null)
            {
                registration.AfterCreation(instance);
            }

            return(instance);
        }