【发布时间】:2015-09-10 08:56:40
【问题描述】:
在使用 Autofac 作为其 IoC 容器的应用程序中,我有一个带有两个类型参数的通用接口:
public interface IMapper<TSource, TTarget>
{
TTarget GetTarget(TSource source);
}
和一个包装器接口,用于根据其输入参数类型动态选择适当的IMapper<TSource, TTarget>:
public interface IDynamicMapper
{
T GetTarget<T>(object source);
}
我希望 IDynamicMapper 的实现在运行时使用 Autofac 找到适当的 IMapper<TSource, TTarget> 组件,该组件的 TSource 等于 source.GetType() 和 TTarget 是T(或T 本身):
public class DynamicMapper : IDynamicMapper
{
private readonly ILifetimeScope _scope;
public DynamicMapper(ILifetimeScope scope)
{
this._scope = scope;
}
T IDynamicMapper.GetTarget<T>(object source)
{
Type sourceType = source.GetType();
Type targetBaseType = typeof(T);
//TODO: find an IMapper<TSource, TTarget> implementation where
// 1) Condition on TSource: typeof(TSource) == sourceType
// 2) Condition on TTarget: targetBaseType.IsAssignableFrom(typeof(TTarget))
// Many implementations can potentially match these criterias,
// choose the 1st one
// (this should not happen if the application is designed correctly)
if (mapper == null)
{
throw new ArgumentException(
"Could not find an IMapper<TSource, TTarget> implementation" +
" for the supplied parameters"
);
}
// call mapper.GetTarget(source) and return the result
// (mapper is probably dynamic, but its runtime type implements
// TTarget IMapper<TSource, TTarget>.GetTarget(TSource source))
}
}
我的所有组件都注册到 Autofac 容器,作为应用程序另一部分中的服务接口(使用程序集扫描记录)。
更新 1
根据 Steven 的相关回答,我更新了如下界面以使用方差:
public interface IMapper<in TSource, out TTarget>
{
TTarget GetTarget(TSource source);
}
我的动态映射器的GetTarget() 方法如下所示:
T IDynamicMapper.GetTarget<T>(object source)
{
Type sourceType = source.GetType();
Type targetBaseType = typeof(TTarget);
Type mapperType = typeof(IMapper<,>).MakeGenericType(sourceType, targetBaseType);
// This fails with ComponentNotRegisteredException
dynamic mapper = this._scope.Resolve(mapperType);
// This also fails (mapper is null):
// IEnumerable<object> mappers = (IEnumerable<object>)this._scope.Resolve(typeof(IEnumerable<>).MakeGenericType(mapperType));
// dynamic mapper = mappers.FirstOrDefault();
// Invoke method
return mapper.GetTarget((dynamic)source);
}
但是,当调用Resolve(mapperType) 或Resolve(typeof(IEnumerable<>).MakeGenericType(mapperType)) 时,组件未解析,尽管它存在于容器的注册中,映射到服务IMapper<TSource, TTarget>。第一次调用引发异常,第二次调用返回空枚举。
【问题讨论】:
标签: c# generics runtime autofac