如何在DryIoc中使用私有构造函数实现?

时间:2016-07-31 18:31:15

标签: c# inversion-of-control dryioc

public class MyClass : IMyClass {
    internal MyClass() {
    }
}

container.Resolve<IMyClass>();

如何将DryIoc配置为使用MyClass的构造函数,以防它是内部的,受保护的甚至是私有的?

1 个答案:

答案 0 :(得分:2)

通过Made.Of指定构造函数信息:

c.Register<Something>(made:
     Made.Of(typeof(Something).GetConstructorOrNull(includeNonPublic: true,  args: argTypes));

更新

这是构造函数选择器的自定义实现。

几个笔记:

  • 不支持Func<..>,但可以通过复制FactoryMethod.ConstructorWithResolvableArguments实施
  • 轻松添加支持
  • issue 313解析后,DryIoc将支持

    using System;
    using System.Linq;
    using System.Reflection;
    using NUnit.Framework;
    
    namespace DryIoc.IssuesTests
    {
        [TestFixture]
        public class SO_question_How_use_implementation_with_private_constructor_in_DryIoc
        {
            public class A { internal A() { } }
            public class B { internal B(A a) { } }
    
            [Test]
            public void Test()
            {
                var c = new Container();
                c.RegisterMany(new[] {typeof(A), typeof(B)},
                    made: Made.Of(SelectMostResolvableConstructor(includeNonPublic: true)));
    
                c.Resolve<B>();
            }
    
            private FactoryMethodSelector SelectMostResolvableConstructor(bool includeNonPublic = false)
            {
                return request =>
                {
                    var implementationType = request.ImplementationType.ThrowIfNull();
                    var ctors = implementationType.GetAllConstructors(includeNonPublic: true).ToArrayOrSelf();
                    if (ctors.Length == 0)
                        return null; // Delegate handling of constructor absence to the Caller code.
                    if (ctors.Length == 1)
                        return FactoryMethod.Of(ctors[0]);
    
                    var ctorsWithMoreParamsFirst = ctors
                        .Select(c => new {Ctor = c, Params = c.GetParameters()})
                        .OrderByDescending(x => x.Params.Length);
    
                    var rules = request.Container.Rules;
                    var parameterSelector = rules.Parameters.And(request.Made.Parameters)(request);
    
                    var matchedCtor = ctorsWithMoreParamsFirst.FirstOrDefault(x =>
                            x.Params.All(p => IsResolvableParameter(p, parameterSelector, request)));
    
                    var ctor = matchedCtor.ThrowIfNull(Error.UnableToFindCtorWithAllResolvableArgs, request).Ctor;
    
                    return FactoryMethod.Of(ctor);
                };
            }
    
            private static bool IsResolvableParameter(ParameterInfo parameter,
                Func<ParameterInfo, ParameterServiceInfo> parameterSelector, Request request)
            {
                var parameterServiceInfo = parameterSelector(parameter) ?? ParameterServiceInfo.Of(parameter);
                var parameterRequest = request.Push(parameterServiceInfo.WithDetails(ServiceDetails.IfUnresolvedReturnDefault, request));
    
                if (parameterServiceInfo.Details.HasCustomValue)
                {
                    var customValue = parameterServiceInfo.Details.CustomValue;
                    return customValue == null
                        || customValue.GetType().IsAssignableTo(parameterRequest.ServiceType);
                }
    
                var parameterFactory = request.Container.ResolveFactory(parameterRequest);
                return parameterFactory != null && parameterFactory.GetExpressionOrDefault(parameterRequest) != null;
            }
        }
    }