在使用可为空的引用类型和泛型类型时收到警告

问题描述 投票:3回答:1

我有一个类似的通用类型,带有一个名为ExecuteAsync的方法,该方法可以返回一个对象或null:

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

public class StoreProcedure<Result, Schema> : IStoreProcedure<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 IStoreProcedure<Content?, API_Content_Get> _api_Content_Get;
    public ContentService(IStoreProcedure<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,我得到以下警告:

enter image description here

我找不到一种方法来显示内容可以为空。我可以这样写,并且不会收到警告,但假定结果值不为null;

private readonly IStoreProcedure<Content, API_Content_Get> _api_Content_Get;
    public ContentService(IStoreProcedure<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;
    }

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

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

c# c#-8.0
1个回答
0
投票

看起来像是您所遵循的语法:

© www.soinside.com 2019 - 2024. All rights reserved.