【发布时间】:2016-04-11 16:28:31
【问题描述】:
我有使用泛型的类和接口。我们使用配置文件来管理 Microsoft Unity for DI 的映射。
类:
namespace Acme.Core
{
public class CommonCache<T> : ICommonCache<T>
{
private string _cacheKey;
public CommonCache(string cacheKey)
{
_cacheKey = cacheKey;
}
public IReadOnlyList<T> GetAll(List<T> dataList)
{
// Code returns IReadOnlyList<T>
}
}
界面:
namespace Acme.Core.Interfaces
{
public interface ICommonCache<T>
{
IReadOnlyList<T> GetAll(List<T> dataList);
}
}
我希望是这样的:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="Acme.Core" />
<namespace name="Acme.Core" />
<namespace name="Acme.Core.Interfaces" />
<container name="Default">
<register type="ICommonCache[*]" mapTo="CommonCache[*]">
<constructor>
<param name="cacheKey" value="*" />
</constructor>
</register>
</container>
</unity>
我知道 * 不是正确的语法,但我的目标是允许为类型传递任何类型,为泛型类型传递 mapTo。对于我想为 cacheKey 参数传递值的构造函数,我有 value="*" 来说明传递任何值的目标。
这个映射的正确语法是什么?
【问题讨论】:
标签: c# .net unity-container