类型&#39; T&#39;必须是参考类型才能将其用作参数&#39; T&#39;在泛型类型或方法中&#39; QueryAPI.Query <t>()

时间:2018-04-23 08:24:26

标签: c#

我正在编写一些代码,这些代码应该在不同的远程对象上使用通用逻辑更新某些字段。因此我使用给定的API。 Test Class是我自己的实现。其他两个类由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 麦克

1 个答案:

答案 0 :(得分:0)

我会从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()
    ...
}
相关问题