Lal*_*lit 1 c# fxcop visual-studio-2010
我在下面的方法中得到代码分析错误.
public static OracleCommand CreateStoredProcedureCommand(string name,
OracleConnection connection)
{
return new OracleCommand(name, connection) { CommandType = CommandType.StoredProcedure };
}
Run Code Online (Sandbox Code Playgroud)
CA2000:Microsoft.Reliability:在方法'StoredProcedureHelper.CreateStoredProcedureCommand(string,OracleConnection)'中,对象'command'未沿所有异常路径放置.在对所有引用超出范围之前,调用System.IDisposable.Dispose对象'command'
如何在不压制这个的情况下解决这个问题?
当对属性的赋值抛出异常时,不会丢弃该对象.试试这个:
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;
}
}
Run Code Online (Sandbox Code Playgroud)