Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称可以简化-Visual Studio 2019覆盖_C#_Asp.net_Visual Studio_Visual Studio 2019 - Fatal编程技术网

C# 名称可以简化-Visual Studio 2019覆盖

C# 名称可以简化-Visual Studio 2019覆盖,c#,asp.net,visual-studio,visual-studio-2019,C#,Asp.net,Visual Studio,Visual Studio 2019,我的ASP.NET项目中有以下代码行: public virtual async Task<bool> Delete(int id) { var entity = LoadById(id); using (IDbConnection cn = new SqlConnection(_conn)) { cn.Open(); /* Do Not Remove <T>

我的ASP.NET项目中有以下代码行:

    public virtual async Task<bool> Delete(int id)
    {
        var entity = LoadById(id);
        using (IDbConnection cn = new SqlConnection(_conn))
        {
            cn.Open();
            /* Do Not Remove <T> Variables, its required even though the compiler notes its not required */
            var result = await cn.DeleteAsync<T>(entity);

            return result;
        }
    }

我的第一个想法是在这行旁边添加一条注释,以防止其他开发人员删除它。然而,我开始思考,必须有某种类型的覆盖,我可以在代码中做,以防止它显示为警告。某种类型的特殊注释或我可以放在行上方的东西,以防止VisualStudio显示该消息。到目前为止,我在网上查过了,什么也找不到。我正在寻找一些我可以放在代码中的东西,而不是VisualStudio中的设置更改

不要添加注释,而是添加一个从一开始就可以阻止警告的注释

[SuppressMessage("Reason", "Whatever the id is", Justification="Do Not Remove <T> Variables, its required even though the compiler notes its not required")]
public virtual async Task<bool> Delete(int id)
{
    var entity = LoadById(id);
    using (IDbConnection cn = new SqlConnection(_conn))
    {
        cn.Open();
        var result = await cn.DeleteAsync<T>(entity);

        return result;
    }
}
[SuppressMessage(“原因”,“无论id是什么”,justify=“不要删除变量,它是必需的,即使编译器注意到它不是必需的”)]
公共虚拟异步任务删除(int-id)
{
var实体=LoadById(id);
使用(IDbConnection cn=newSQLConnection(_conn))
{
cn.Open();
var result=wait cn.deleteAync(实体);
返回结果;
}
}

它是否在消费者电话中发出警告?这可能是因为编译器可以根据实体的类型推断类型,因此泛型调用的类型注释是多余的。LoadById返回的类型是什么?
LoadById
?如果阅读答案,您将看到可以仅禁用一行警告。当删除泛型类型参数时,是用
对象谓词
参数而不是
T实体
参数调用扩展方法吗?会得到什么样的警告?Intellisense,编译,FxCop其他东西?试过了,这个,它不起作用:(它怎么不起作用?编译器错误?你到底在属性中放了什么?不管怎样,我的ID错了,它起作用了:[SuppressMessage(“Reason”,“IDE0001”,Justification=“不要删除变量,它是必需的,即使编译器注意到它不是必需的”)]
var result = await cn.DeleteAsync<T>(entity);
var result = await cn.DeleteAsync(entity);
[SuppressMessage("Reason", "Whatever the id is", Justification="Do Not Remove <T> Variables, its required even though the compiler notes its not required")]
public virtual async Task<bool> Delete(int id)
{
    var entity = LoadById(id);
    using (IDbConnection cn = new SqlConnection(_conn))
    {
        cn.Open();
        var result = await cn.DeleteAsync<T>(entity);

        return result;
    }
}