使用统一xml配置向嵌套泛型注册接口 [英] Using unity xml config to register an interface with nested generics
问题描述
如果我有一个实现通用接口的类,那么使用unity xml config配置它就可以。
If I have a class that implements a generic interface it works fine to configure it using the unity xml config.
public interface IReader<T> { }
public class Fund { }
public class FundReader : IReader<Fund> { }
和unity xml:
and the unity xml:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<namespace name="System.ComponentModel" />
<namespace name="TestUnityIssue" />
<assembly name="TestUnityIssue" />
<container>
<register type="IReader[Fund]" mapTo="FundReader" />
</container>
</unity>
这个工程只需使用以下代码:
And this works just using the following code:
var container = new UnityContainer().LoadConfiguration();
var fundReader = container.Resolve<IReader<Fund>>();
然而,在某些情况下,在阅读器中使用了一种类型的包装。例如,添加以下两个类:
However, in some cases there is a wrapper around the type used in the reader. For example, adding the following two classes:
public class Wrapper<T> { }
public class WrappedFundReader : IReader<Wrapper<Fund>> { }
如果我将以下内容添加到统一配置中:
and if I add the following to the unity config:
<register type="IReader[Wrapper[Fund]]" mapTo="WrappedFundReader" />
然后尝试使用以下方法解决它:
and then try to resolve it using:
var wrappedReader = container.Resolve<IReader<Wrapper<Fund>>>();
我收到以下异常:
I get the following exception:
InvalidOperationException - The current type,
TestUnityIssue.IReader`1[TestUnityIssue.Wrapper`1[TestUnityIssue.Fund]],
is an interface and cannot be constructed. Are you missing a type
mapping?
我可以通过使用代码而不是xml来配置它:
I can get around this by configuring it using code instead of xml:
container.RegisterType<IReader<Wrapper<Fund>>, WrappedFundReader>();
或者我可以创建一个介于两者之间并使用它的接口:
or I can create an interface that goes between and use that instead:
public interface IWrappedReader<T> : IReader<Wrapper<T>> { }
public class WrappedFundReader : IWrappedReader<Fund>
,配置将变为:
and the config would change to:
<register type="IWrappedReader[Fund]" mapTo="WrappedFundReader" />
它仍然会给我一个实例,可以转换为 IReader< Wrapper< ; Fund>>
,但它似乎应该能够使这个工作与统一配置。
It will still give me an instance that I can cast to IReader<Wrapper<Fund>>
, but it seems like I should be able to make this work with the unity config.
我错过了什么来完成这项工作?
What am I missing to make this work?
(如果还尝试创建特定的别名,并且无法使其工作)
(If also tried creating specific aliases and wasn't able to get that to work either)
推荐答案
别名为我工作...
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="WrappedFund" type="TestUnityIssue.Wrapper`1[[TestUnityIssue.Fund, TestUnityIssue, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], TestUnityIssue, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<namespace name="TestUnityIssue" />
<assembly name="TestUnityIssue" />
<container>
<register type="IReader[Fund]" mapTo="FundReader" />
<register type="IReader[WrappedFund]" mapTo="WrappedFundReader" />
</container>
</unity>
这些都解决了...
var fundReader = container.Resolve<IReader<Fund>>();
var wrappedReader = container.Resolve<IReader<Wrapper<Fund>>>();
并且根据您的情况,您可能会得到比完整的AssemblyQualifiedName更少的值。 。
and depending on your situation, you may be able to get away with less than the full AssemblyQualifiedName...
<alias alias="WrappedFund" type="TestUnityIssue.Wrapper`1[[TestUnityIssue.Fund, TestUnityIssue]], TestUnityIssue" />
这篇关于使用统一xml配置向嵌套泛型注册接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!