这个警告非常烦人,我已经阅读了一些建议方法,但它们似乎没有用。
据我所知,我已经为下面的代码中的所有场景提供了帮助,但我仍然会收到警告。我正在使用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;
}
}
}
答案 0 :(得分:3)
下面:
if (value != null)
result = (value as Configuration).Solutions.LicenseModules;
您将新值分配给result
,而已经处理了您在此处创建的实例:
result = new LicenseModules();
在分配新的
之前,您应该创建一个块并处理旧结果值: if (value != null) {
result.Dipose();
result = (value as Configuration).Solutions.LicenseModules;
}