简体   繁体   English

C#CA2000在丢失范围之前处置对象

[英]C# CA2000 Dispose Object Before Losing Scope

This is my code, it gives me CA2000 on "new DataTable()..." and "new DataColumn()..."s 这是我的代码,它给了我CA2000“new DataTable()...”和“new DataColumn()...”

usersDS.Tables.Add(new DataTable()
{
    TableName = "Users",
    Columns = { new DataColumn() { ColumnName = "Handle", DataType = typeof(string) }, new DataColumn() { ColumnName = "Nickname" ,DataType = typeof(string) } }
});

Is it possible to fix without declaring variables? 是否可以在不声明变量的情况下进行修复?

This is nearly a duplicate of How to fix a CA2000 IDisposable C# compiler warning, when using a global cache . 当使用全局缓存时,这几乎与如何修复CA2000 IDisposable C#编译器警告重复。 Maybe it should be considered a duplicate of that one. 也许它应该被认为是那个的重复。 I'm not sure. 我不确定。

Code Analysis is legitimately complaining that it is theoretically possible for the method to complete without the IDisposable object being disposed and without it being safely stored somewhere else. 代码分析合法地抱怨理论上可以在没有IDisposable对象的情况下完成该方法,并且不将其安全地存储在其他地方。 The latter can happen if an exception occurs during the initialization of the DataTable object or adding the DataTable object to the usersDS.Table object (whatever that is). 如果在DataTable对象初始化期间发生异常或将DataTable对象添加到usersDS.Table对象(无论是什么),则会发生后者。

If you can guarantee that no exception will be thrown here, then IMHO it is perfectly fine to suppress the CA warning. 如果你可以保证不会抛出任何异常,那么恕我直言,完全可以抑制CA警告。 In that case, you know more than CA can, and you're promising you know what you're doing. 在这种情况下,你知道的不仅仅是CA,而且你承诺你知道你在做什么。

If you can't make the guarantee, then no…it's not possible to fix the warning without introducing local variables so that you're able to dispose the object in the event of an exception. 如果你不能做出保证,那么就没有...在不引入局部变量的情况下修复警告是不可能的,这样你就可以在发生异常时处理对象。 For example: 例如:

DataTable dataTable = null;
DataColumn dataColumn1 = null, dataColumn2 = null;

try
{
    dataColumn1 = new DataColumn() { ColumnName = "Handle", DataType = typeof(string) };
    dataColumn2 = new DataColumn() { ColumnName = "Nickname", DataType = typeof(string) };
    dataTable = new DataTable()
    {
        TableName = "Users",
        Columns = { dataColumn1, dataColumn2 }
    };
    usersDS.Tables.Add(dataTable);
}
catch
{
    if (dataTable != null)
    {
        dataTable.Dispose();
    }
    if (dataColumn1 != null)
    {
        dataColumn1.Dispose();
    }
    if (dataColumn2 != null)
    {
        dataColumn2.Dispose();
    }
    throw;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#CA2000:使用FileStream / XmlTextReader在丢失范围之前处置对象 - C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader 控制器上的“ CA2000在失去作用域之前先放置对象” - “CA2000 Dispose objects before losing scope” on controller “ CA2000丢失范围之前处置对象”原因不明 - “CA2000 Dispose objects before losing scope ” for unknown reason CA2000在失去DirectoryEntry对象的MVC项目中的作用域之前,先处理对象 - CA2000 Dispose objects before losing scope in MVC project for DirectoryEntry object “ CA2000:在失去作用域之前处理对象”,构建Unity容器 - “CA2000: Dispose object before losing scope” building Unity container FxCop 10.0找不到CA2000“在丢失范围之前处置对象” - FxCop 10.0 can't find CA2000 “Dispose objects before losing scope” 我是否需要在这两个XmlDataSource对象上都调用.Dispose()? “ CA2000在失去作用域之前先处理对象”告诉我.Dispose() - Do I need to call .Dispose() on both of these XmlDataSource objects? “CA2000 Dispose objects before losing scope” tells me to .Dispose() it CA2000:处理对象警告 - CA2000: Dispose object warning CA2000:Microsoft.Reliability:在对所有引用超出范围之前调用System.IDisposable.Dispose对象'dt' - CA2000 : Microsoft.Reliability : Call System.IDisposable.Dispose on object 'dt' before all references to it are out of scope CA2000:Microsoft.Reliability在对象的所有引用超出范围之前,先对对象调用System.IDisposable.Dispose。 - CA2000 : Microsoft.Reliability call System.IDisposable.Dispose on object before all references to it are out of scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM