我有一个基于查询参数和查询结果运行查询的界面
public interface IQueryParam {}
public interface IQueryResult {}
public interface IQuery<in TQueryParam, out TResult>
where TQueryParam : IQueryParam
where TResult : IQueryResult
{
TResult RunQuery(TQueryParam query);
}
public class GetBushCodes : IQuery<GetalBushCodesParam, ReturnBushCode[]>
{
ReturnBushCode[] RunQuery(GetalBushCodesParam param) { /*TODO*/ }
}
我希望任何继承自IQuery的类都能够返回一个IQueryResult或一个IEnumerable或Array的单个实例IQueryResult。我当前的设置不允许这样做,编译器对我大喊大叫GetBushCodes:
The type 'System.Collections.Generic.IEnumerable<App.Domain.Queries.ReturnBushCode>'
cannot be used as type parameter 'TResult' in the generic type or
method 'App.Domain.Queries.IQuery<TQueryParam,TResult>'. There is no implicit
reference conversion from
'System.Collections.Generic.IEnumerable<App.Domain.Queries.ReturnBushCode>' to
'App.Domain.Queries.IQueryResult'.
我不想使用该设置IQueryParam<TResult> where TResult : IQueryResult,因为这似乎具有限制性,我想尝试保持IQueryResult约束,IQuery因为GetBushCodes如果我摆脱了约束,则编译。