我正在编写一些代码,这些代码应该在不同的远程对象上使用公共逻辑更新一些字段。因此,我使用给定的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();
...
}对此有什么想法吗?
提前谢谢。
KR迈克
发布于 2018-04-23 08:33:37
我将在这里从T中删除Foo(),因为父类Test<T>已经是泛型的。
您还应该添加一个new()约束,否则会出现另一个错误,因为QueryAPI需要一个具有默认构造函数的类型。
此外,一些重命名包含Async也是合适的。
public class Test<T> where T : class, UnicontaBaseEntity, new()
{
private async Task FooAsync(QueryAPI queryAPI, CrudAPI crudAPI, SyncSettings syncSettings)
{
Task<T[]> result = await queryAPI.QueryAsync<T>();
}
}
public interface UnicontaBaseEntity : UnicontaStreamableEntity
{
int CompanyId { get; }
Type BaseEntityType();
}
public class QueryAPI : BaseAPI
{
...
public Task<T[]> QueryAsync<T>() where T : class, UnicontaBaseEntity, new()
...
}https://stackoverflow.com/questions/49976483
复制相似问题