Exemplo n.º 1
0
        public TInterface ResolveWithArguments <TInterface>(params object[] arguments)
        {
            try
            {
                object kernelTarget = kernel.GetBindings(typeof(TInterface)).First().ProviderCallback.Target;

                Type implementationType = (Type)kernelTarget.GetType().GetField("prototype").GetValue(kernelTarget);

                var constructor = implementationType.GetConstructor(arguments.Select(arg => arg.GetType()).ToArray());

                List <ConstructorArgument> constructorArguments = new List <ConstructorArgument>();

                int i = 0;

                foreach (var parameter in constructor.GetParameters())
                {
                    constructorArguments.Add(new ConstructorArgument(parameter.Name, arguments[i++]));
                }

                return(kernel.Get <TInterface>(constructorArguments.ToArray()));
            }
            catch
            {
                throw new NoRegisteredImplementationException();
            }
        }