gpt4 book ai didi

c# - CA2000 : Microsoft. 可靠性对象未沿所有异常路径处理

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:50 25 4
gpt4 key购买 nike

我在以下方法中遇到代码分析错误。

    public static OracleCommand CreateStoredProcedureCommand(string name,
OracleConnection connection)
{
return new OracleCommand(name, connection) { CommandType = CommandType.StoredProcedure };
}

CA2000 : Microsoft.Reliability : In method 'StoredProcedureHelper.CreateStoredProcedureCommand(string, OracleConnection)', object 'command' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'command' before all references to it are out of scope

如何在不抑制的情况下解决这个问题?

最佳答案

当对属性的赋值抛出异常时,对象不会被释放。试试这个:

public static OracleCommand CreateStoredProcedureCommand(string name,
OracleConnection connection)
{
OracleCommand result = new OracleCommand(name, connection);
try
{
result.CommandType = CommandType.StoredProcedure;
return result;
}
catch
{
result.Dispose();
throw;
}
}

关于c# - CA2000 : Microsoft. 可靠性对象未沿所有异常路径处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9207801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com