UnityContainer和内部构造 [英] UnityContainer and internal constructor

查看:204
本文介绍了UnityContainer和内部构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部的构造函数的类,并希望从Unity(2.0)解决它。

I have a class with internal constructor and want to Resolve it from Unity (2.0).

public class MyClass {
    internal MyClass(IService service) {
    }
}

然后我在做

_container.Resolve<MyClass>();

当我这样做,我有一个例外

when I do so I have an exception

Exception is: InvalidOperationException - The type MyClass cannot be constructed. 



IService注册,唯一的问题是,构造函数是内部的。
我真的希望这个类是公共的,但我想它只能通过工厂可创建的(在我实际调用 container.Resolve< MyClass的>())。

有没有一种方法,使团结看到内部构造?像InternalsVisibleTo什么?

Is there a way to make Unity see that internal constructor? Like InternalsVisibleTo or something?

推荐答案

我挖一点点变成你怎么可能会延长团结为此目的,并发现了一些有趣的信息。

I dug a little into how you might extend Unity for this purpose, and found some interesting information.

首先,它似乎是统一选择由内部解决的 IConstructorSelectorPolicy 来使用的构造函数。包括在团结是公共抽象类ConstructorSelectorPolicyBase< TInjectionConstructorMarkerAttribute> ,其中包括这颗宝石:

First, it seems that Unity selects which constructor to use by internally resolving an IConstructorSelectorPolicy. Included in Unity is the public abstract class ConstructorSelectorPolicyBase<TInjectionConstructorMarkerAttribute>, which includes this gem:

/// <summary>
/// Choose the constructor to call for the given type.
/// </summary>
/// <param name="context">Current build context</param>
/// <param name="resolverPolicyDestination">The <see cref='IPolicyList'/> to add any
/// generated resolver objects into.</param>
/// <returns>The chosen constructor.</returns>
public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
{
    Type typeToConstruct = context.BuildKey.Type;
    ConstructorInfo ctor = FindInjectionConstructor(typeToConstruct) ?? FindLongestConstructor(typeToConstruct);
    if (ctor != null)
    {
        return CreateSelectedConstructor(context, resolverPolicyDestination, ctor);
    }
    return null;
}



FindInjectionConstructor 及公司在这一类,它最终最终调用 Type.GetConstructors (过载不带任何参数,私有静态方法只返回公共构造函数)。这告诉我,如果你能安排统一使用自己的构造选择的政策,这将能够选择任何构造函数,你是金色的。

FindInjectionConstructor and company are private static methods in this class which ultimately end up calling Type.GetConstructors (the overload with no parameters, which only returns public constructors). This tells me that if you can arrange for Unity to use your own constructor selector policy, which would be able to select any constructor, you are golden.

有<一个HREF =http://msdn.microsoft.com/en-us/library/ff660845%28v=PandP.20%29.aspx>有关如何实现并利用自己的饭盒扩展好的文档,所以我想它很可能使自己的 CustomConstructorSelectorPolicy 包含的 DefaultUnityConstructorSelectorPolicy (从抽象基类派生,它是默认,除非你注册别的东西)和<一个HREF =https://unity.svn.codeplex.com/svn/Unity/Unity/Src/ObjectBuilder/Strategies/BuildPlan/Creation/ConstructorSelectorPolicyBase.cs> ConstructorSelectorPolicyBase (从这个直接推导可能不会很好地工作,因为主要的方法是不虚拟,但你可以重用的代码)。

There is good documentation about how to make and utilize your own container extensions, so I imagine it's quite possible to make your own CustomConstructorSelectorPolicy that includes the relevant portions of DefaultUnityConstructorSelectorPolicy (which derives from the abstract base class and is the default unless you register something else) and ConstructorSelectorPolicyBase (deriving from this directly would probably not work well because key methods are not virtual, but you can reuse the code).

所以我可以说这是可行的与麻烦适量的,但最终的结果将是非常纯洁从工程的角度来看。

Therefore I 'd say it's doable with a moderate amount of hassle, but the end result would be quite "pure" from an engineering point of view.

这篇关于UnityContainer和内部构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆