简体   繁体   English

当接口有多个通用参数时,如何解决“无法隐式转换[type]到[interface]”错误?

[英]How do I solve the “cannot implicitly convert [type] to [interface]” error when the interface has multiple generic parameters?

I have the following interfaces: 我有以下接口:

public interface IQueryHandler<in TQuery, out TResult> where TResult : class 
                                                where TQuery : IQuery<TResult>
{
    TResult Handle(TQuery query);
}

public interface IQuery<TResult> // Doesn't require anything special, just used to guarantee that IQueryHandlers get the right parameters.
{
}

It's intended to be used by IQueryHandlers which will take in an IQuery<TResult> which defines a query that returns an object of type TResult . 它旨在由IQueryHandlers ,它将接收IQuery<TResult> ,它定义一个返回TResult类型对象的查询。 The IQueryHandler 's Handle method then returns a TResult . 然后IQueryHandlerHandle方法返回一个TResult

I have the interface implemented on a DataQueryHandlers class: 我在DataQueryHandlers类上实现了接口:

public class DataQueryHandlers : IQueryHandler<GetDataById, SomeData>
{
    private IDataSource source;

    public DataQueryHandlers(IDataSource source)
    {
        this.source = source
    }

    public SomeData Handle(GetDataById query)
    {
        // Get the data here and return SomeData object
    }
}

where SomeData is a data entity, and GetDataById is an IQuery<SomeData> 其中SomeData是数据实体, GetDataByIdIQuery<SomeData>

However, when I try to instantiate a specific instance: 但是,当我尝试实例化一个特定的实例时:

private IQueryHandler<IQuery<SomeData>, SomeData> dataQueryHandlers;
private IDataSource source;

source = new DataSource(); // some data provider

dataQueryHandlers = new DataQueryHandlers(datasource); // This line won't compile

I get a compiler error: 我收到编译器错误:

Cannot implicitly convert type DataQueryHandlers to IQueryHandler<IQuery<SomeData>, SomeData> . 无法将类型DataQueryHandlers隐式转换为IQueryHandler<IQuery<SomeData>, SomeData> An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否错过了演员?)

I'm sure this is a covariant/contravariant related problem, but I fail to see where the mismatch is. 我确定这是一个协变/逆变相关的问题,但我没有看到不匹配的位置。 Is there a problem with my in/out generic modifiers? 我的输入/输出通用修饰符有问题吗? Is what I'm trying to do fundamentally incorrect in some way? 从某种意义上说,我试图从根本上做错吗? Am I missing some obvious " hair on a fish " scenario here? 这里错过了一些明显的“ 鱼头发 ”情景吗?

You should change your derived class to generic one to be able to do it. 您应该将派生类更改为通用类以便能够执行此操作。 Change it to this: 把它改成这个:

public class DataQueryHandlers<in TQuery, out TResult> : IQueryHandler<TQuery, TResult> where TResult : class where TQuery : IQuery<TResult>
{
    private IDataSource source;

    public DataQueryHandlers(IDataSource source)
    {
        this.source = source
    }

    public TResult Handle(TQuery query)
    {
        // Get the data here and return TResult object
    }
}

More about Generic Classes you can find MSDN 有关Generic Classes的更多信息,您可以找到MSDN

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

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