在 Unity 配置中注册泛型类型

发布于 2024-10-09 07:56:31 字数 2018 浏览 0 评论 0原文

哦!这是一个非常奇怪的问题。我打算注册这两种类型,但是没有什么可以很好地工作。

<unity>
    <typeAliases>
        <typeAlias alias="IEqualityComparer`1"
                   type="System.Collections.Generic.IEqualityComparer`1, mscorlib" />
        <typeAlias alias="singleton"
                   type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />            
        <typeAlias alias="EqualityComparer`1"
                   type="System.Collections.Generic.EqualityComparer`1, mscorlib" />
    </typeAliases>
    <containers>
        <container>
            <types>
                <register type="IEqualityComparer`1"
                          mapTo="EqualityComparer`1">
                    <lifetime type="singleton" />
                </register>
            </types>
        </container>
    </containers>
</unity>

这是我的控制器:

public class MyController : MyExtendedController {
    private readonly IEqualityComparer<int> _fakeComparer;

    public ResourcesController(IEqualityComparer<int> fakeComparer) {
        _fakeComparer = fakeComparer;
    }
}

Unity 在解析控制器时抛出异常:

Resolution of the dependency failed, type = "MyController", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type EqualityComparer`1 cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:
Resolving MyController,(none)
Resolving parameter "fakeComparer" of constructor   MyController(System.Collections.Generic.IEqualityComparer`1[[int, mscorelib]] fakeComparer)
Resolving System.Collections.Generic.EqualityComparer`1[int, mscorelib],(none) (mapped from System.Collections.Generic.IEqualityComparer`1[int, mscorelib], (none))

如有任何建议,我们将不胜感激;)

Oh! this is very strange issue. I'm going to register these two types, but there is nothing to be work well.

<unity>
    <typeAliases>
        <typeAlias alias="IEqualityComparer`1"
                   type="System.Collections.Generic.IEqualityComparer`1, mscorlib" />
        <typeAlias alias="singleton"
                   type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />            
        <typeAlias alias="EqualityComparer`1"
                   type="System.Collections.Generic.EqualityComparer`1, mscorlib" />
    </typeAliases>
    <containers>
        <container>
            <types>
                <register type="IEqualityComparer`1"
                          mapTo="EqualityComparer`1">
                    <lifetime type="singleton" />
                </register>
            </types>
        </container>
    </containers>
</unity>

And this is my controller:

public class MyController : MyExtendedController {
    private readonly IEqualityComparer<int> _fakeComparer;

    public ResourcesController(IEqualityComparer<int> fakeComparer) {
        _fakeComparer = fakeComparer;
    }
}

And exception throws by Unity when resolving Controller:

Resolution of the dependency failed, type = "MyController", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type EqualityComparer`1 cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:
Resolving MyController,(none)
Resolving parameter "fakeComparer" of constructor   MyController(System.Collections.Generic.IEqualityComparer`1[[int, mscorelib]] fakeComparer)
Resolving System.Collections.Generic.EqualityComparer`1[int, mscorelib],(none) (mapped from System.Collections.Generic.IEqualityComparer`1[int, mscorelib], (none))

Any suggestions would be appreciated ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-10-16 07:56:31

更新答案

原因是 EqualityComparer 是一个抽象类,如您所见 MSDN。 Unity无法实例化抽象类,您必须提供具体的实现。



第一个答案

您是否配置了服务器:

var container = new UnityContainer();
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);

Updated answer

The reason is that EqualityComparer<T> is an abstract class, as you can see on MSDN. Unity cannot instantiate abstract classes, you have to provide a concrete implementation.



First answer

Did you have configured your server:

var container = new UnityContainer();
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文