【发布时间】:2018-04-23 08:24:25
【问题描述】:
我正在编写一些代码,这些代码应该使用不同远程对象上的通用逻辑更新某些字段。因此我使用给定的 API。测试类是我自己的实现。其他两个类由 API 提供。 当我编写以下代码时,我得到了错误
The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'QueryAPI.Query<T>()
代码:
public class Test<T> where T : class, UnicontaBaseEntity
{
private async Task Foo<T>(QueryAPI queryAPI, CrudAPI crudAPI, SyncSettings syncSettings)
{
Task<T[]> result = await queryAPI.Query<T>();
}
}
public interface UnicontaBaseEntity : UnicontaStreamableEntity
{
int CompanyId { get; }
Type BaseEntityType();
}
public class QueryAPI : BaseAPI
{
...
public Task<T[]> Query<T>() where T : class, UnicontaBaseEntity, new();
...
}
对此有何想法?
提前致谢。
韩语 迈克
【问题讨论】:
-
Test<T>->Foo<T>没有意义。由于该类是泛型的,因此将方法设为非泛型。 -
编译器甚至会为此生成警告。在
Foo方法范围内的T隐藏了Test类的T。
标签: c#