在将泛型引用类型与泛型类型一起使用时收到警告 - c#

我有一个类似于以下的泛型类型,它带有一个名为ExecuteAsync()的方法,该方法可以返回一个对象或null


public interface IStoredProcedure<Result, Schema>
    where Result : IBaseEntity
    where Schema : IBaseSchema
{
    Task<Result> ExecuteAsync(Schema model);
}

public class StoredProcedure<Result, Schema> : IStoredProcedure<Result, Schema>
    where Result : IBaseEntity
    where Schema : IBaseSchema
{
    public async Task<Result> ExecuteAsync(Schema model){
        //I use QueryFirstOrDefaultAsync of Dapper here, which returns an object or null
        throw new NotImplementedException();
    }
}

我在服务中按如下方式使用它:

public interface IContentService
{
    Task<Content?> Get(API_Content_Get schema);
}

public class ContentService : IContentService
{
    private readonly IStoredProcedure<Content?, API_Content_Get> _api_Content_Get;

    public ContentService(IStoredProcedure<Content?, API_Content_Get> api_Content_Get)
    {
        _api_Content_Get = api_Content_Get;
    }

    public async Task<Content?> Get(API_Content_Get schema)
    {
        Content? result = await _api_Content_Get.ExecuteAsync(schema);
        return result;
    }
}

如果我没有在?中添加ContentService以显示内容可以是null,则会收到以下警告:

在将泛型引用类型与泛型类型一起使用时收到警告 - c#

我找不到一种方法来显示内容可以是null。我可以将其编写如下,并且不会收到任何警告,但它会假定结果值不是null

private readonly IStoredProcedure<Content, API_Content_Get> _api_Content_Get;
public ContentService(IStoredProcedure<Content, API_Content_Get> api_Content_Get)
{
    _api_Content_Get = api_Content_Get;
}

public async Task<Content?> Get(API_Content_Get schema)
{
    Content? result = await _api_Content_Get.ExecuteAsync(schema);
    return result;
}

我知道这只是一个警告,不会影响该过程。但是我有什么可以解决的吗?

我认为应该修复此新功能中的错误。

参考方案

看起来这是您要遵循的语法:

public interface IStoreProcedure<Result, Schema>
where Result : IBaseEntity?
where Schema : IBaseSchema {
   Task<Result> ExecuteAsync(Schema model);
}

似乎默认情况下,在可为空的上下文中,类型约束意味着不可空性,因此要获得可空性,您必须在类型约束中添加?

重复使用Google Api Bearer令牌来访问用户的云端硬盘 - javascript

我有以下几点: gapi.auth.authorize( { client_id: CLIENT_ID, scope: SCOPES, immediate: false }, handleAuthResult); 这使我可以访问access_token:目标是使该应用程序的用户可以授予对我的应用程序的访问权限,以使用其Google驱动器存储其内容。我需要能够…

JSON SCHEMA PATTERN逗号分隔列表 - python

我的json模式中具有以下模式,并且我需要根据以下模式包含逗号分隔的值。当前模式只能像DV2一样处理一种模式所以我应该如何修改我的模式以包括多个字符串,如下所示,但它应该与声明的模式匹配。例如:“ DV2”,“ DEV1”,“ DEV3”,“ ST”, "ENVIRONMENT": { "type": "st…

我需要帮助将此REST API Curl命令转换为Python请求 - python

我在这里是新手,老实说对所有编码都是新手。我正在尝试创建一个Pyton脚本,以使用REST API从Request Tracker资产数据库中搜索项目。到目前为止,我得到了以下Curl命令:curl -X POST \ -H "Content-Type: application/json" \ -d '[{ "fiel…

ASP.NET Web API 2中AuthorizeAttribute发生了什么变化? - c#

我已经将项目和一组单元测试从ASP.NET Web API升级到ASP.NET Web API 2。我们将自定义DelegatingHandler用于自定义身份验证机制。它将Thread.CurrentPrincipal和HttpContext.Current.User设置为适当的System.Security.Claims.ClaimsPrincipal。…

检查Optional中是否存在null属性,并返回String Java Stream API - java

我有以下class Person private String firstName; private String familyName; // Setters and Getters 我有以下方法public String getFullName(Optional<Person> persons) { return persons .map(p…