Sto usando .net e ho bisogno di ottenere del testo html, quindi ho pensato di utilizzare HtmlTextWriter e StringWriter per ottenere l'html ben formato. Ma nonostante tutti i diversi modi in cui scrivo il codice, ricevo comunque degli avvisi dall'analizzatore di codici statici (utilizzando Microsoft All Rules). Negli esempi di codice qui sotto, mostro l'avvertenza dell'analizzatore del codice in un commento. Per semplificare il codice, in realtà non faccio alcuna chiamata a HtmlTextWriter (vedrai un commento a tale effetto in ogni funzione). Come posso scrivere il codice correttamente per evitare gli avvertimenti?Come utilizzare StringWriter e HtmlWriter insieme senza avvisi di analisi del codice
// CA2000 : Microsoft.Reliability : In method 'Default.Func1()', object 'stringWriter' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'stringWriter' before all references to it are out of scope.
public static string Func1()
{
string html;
StringWriter stringWriter;
using (var writer = new HtmlTextWriter(stringWriter = new StringWriter()))
{
// You would do some stuff with the writer here, but not for this example.
html = stringWriter.ToString();
}
return html;
}
// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in method 'Default.Func2()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 45
public static string Func2()
{
string html;
StringWriter stringWriter = null;
try
{
using (var writer = new HtmlTextWriter(stringWriter = new StringWriter()))
{
// You would do some stuff with the writer here, but not for this example.
html = stringWriter.ToString();
}
}
finally
{
if (stringWriter != null)
stringWriter.Dispose();
}
return html;
}
// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func3()'. To avoid generating a System.ObjectDisposedException
// you should not call Dispose more than one time on an object.: Lines: 61
public static string Func3()
{
string html;
using (var stringWriter = new StringWriter())
{
using (var writer = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.
html = stringWriter.ToString();
}
}
return html;
}
// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func4()'. To avoid generating a System.ObjectDisposedException you
// should not call Dispose more than one time on an object.: Lines: 77
public static string Func4()
{
string html;
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.
html = stringWriter.ToString();
}
}
return html;
}
// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func5()'. To avoid generating a System.ObjectDisposedException you
// should not call Dispose more than one time on an object.: Lines: 100
public static string Func5()
{
string html;
StringWriter stringWriter = null;
try
{
stringWriter = new StringWriter();
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.
html = stringWriter.ToString();
}
}
finally
{
if (stringWriter != null)
stringWriter.Dispose();
}
return html;
}
un sacco di commenti interessanti su [CA2202] (http://msdn.microsoft.com/en-us/library/ms182334.aspx) su MSDN –
Purtroppo nessuno dei commenti alla Il collegamento CA2202 in precedenza ha indirizzato l'avviso in questa particolare coppia di classi quando li ho provati. –
In [un altro post] (http://stackoverflow.com/questions/3831676/ca2202-how-to-solve-this-case), Hans Peter scrive "Questi non sono esattamente bug reali, queste classi .NET sono resistenti a più chiamate Dispose(). " Con questo in mente sono tentato di fare il doppio utilizzo come Func3 o Func4 e sopprimere l'avviso CA2202. –