繁体   English   中英

具有接口类型的键的字典,获取实现接口的键

[英]Dictionary with a key of interface type, get keys where interface is implemented

我有interface类型和Func的字典。 IBaseIAIBIC

var _dictionary =
    new Dictionary<Type, Func<IBase, IEnumerable<IResult>>>
    {
        {typeof(IA), p => _mapper.Map(p as IA)},
        {typeof(IB), p => _mapper.Map(p as IB)},
        {typeof(IC), p => _mapper.Map(p as IC)}
    };

IA (或IB等)的具体实例传递给方法,如何获得与实例实现的匹配接口相对应的Func

public IEnumerable<IResult> Resolve(IBase theInstance)
{
    // This fails because theInstance.GetType() result, is not the interface type
    if (!_dictionary.TryGetValue(theInstance.GetType(), out var func)) return Enumerable.Empty<IResult>();

    var result = func.Invoke(theInstance);

    return result;
}

我试图避免使用每种接口类型的switch语句。

Type公开一个GetInterfaces()方法,该方法返回Type实现的所有接口。

如果您的类型仅实现一个接口,则可能会起作用,但是如果实现了更多的接口,则可能需要重新编写解决方案。 也许工厂将为指定的接口类型返回一个映射器?

相关阅读内容: https : //docs.microsoft.com/zh-cn/dotnet/api/system.type.getinterfaces?view=netframework-4.7.2

这将不起作用,因为字典类型与实例类型不匹配。

如果检查字典,您会发现存储为键的类型是接口的类型:

{Name = "IA" FullName = "SomeNameSpace.IA"}

因此,当您尝试使用实例的类型获取值时,没有匹配项,并且失败。

如果您希望此方法有效,则需要在字典中注册实例的类型以进行适当的解析:

var _dictionary = new Dictionary<Type, Func<IBase, IEnumerable<IResult>>>
{
    {typeof(IA), p => _mapper.Map(p as IA)},
    {typeof(IB), p => _mapper.Map(p as IB)},
    {typeof(IC), p => _mapper.Map(p as IC)},
    //This would make it work
    {typeof(SomeInstanceType), p=> _mapper.Map(p as IA) }
};

这显然不是很实用。

相反,您可以尝试找出类型是否实现您的词典所说明的接口之一:

var interfaces = theInstance.GetType().GetInterfaces();
var key = _dictionary.Keys.Where(k => interfaces.Contains(k)).FirstOrDefault();

if (key == null) 
    return Enumerable.Empty<IResult>();

var map= _dictionary[key];
var result = map(theInstance);
return result;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM