![](/img/trans.png)
[英]CA2000 : Microsoft.Reliability : Call System.IDisposable.Dispose on object 'dt' before all references to it are out of scope
[英]CA2000 : Microsoft.Reliability call System.IDisposable.Dispose on object before all references to it are out of scope
这个警告非常令人讨厌,我已经阅读了许多帖子,提出了解决该问题的方法,但是它们似乎没有用。
据我所知,我已经满足了下面代码中的所有情况,但仍然收到警告。 我正在使用VS2010。 我很想抑制它,但是如果它不太丑陋,我宁愿修复它。 有什么建议么?
private LicenseModules CreateDataSource
{
get
{
// set default result
LicenseModules result = null;
try
{
result = new LicenseModules();
// lookup the "Tools" class in the current entry assembly
var lookup = Assembly.GetEntryAssembly().GetTypes().FirstOrDefault(item => item.Name.Equals("Tools", StringComparison.Ordinal));
// ensure the lookup result is valid
if (lookup != null)
{
// lookup the Configuration property
var prop = lookup.GetProperty("Configuration", BindingFlags.Static | BindingFlags.Public);
// validate the lookup result
if (prop != null)
{
// read the current Configuration value
object value = prop.GetValue(lookup, null);
// ensure the read value is valid
if (value != null)
result = (value as Configuration).Solutions.LicenseModules;
}
}
// return current result
return result;
}
catch (Exception)
{
if (result != null)
{
result.Dispose();
}
throw;
}
}
}
这里:
if (value != null)
result = (value as Configuration).Solutions.LicenseModules;
您可以为result
分配一个新值, result
无需处理您在此处创建的实例:
result = new LicenseModules();
在分配新值之前,您应该创建一个块并处理旧的结果值:
if (value != null) {
result.Dipose();
result = (value as Configuration).Solutions.LicenseModules;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.