Tôi đang sử dụng .net và cần lấy một số văn bản html, vì vậy tôi nghĩ mình sẽ sử dụng HtmlTextWriter và StringWriter cùng nhau để nhận dạng html đúng. Nhưng mặc dù tất cả các cách khác nhau tôi viết mã, tôi vẫn nhận được cảnh báo từ trình phân tích mã tĩnh (sử dụng Microsoft All Rules). Trong các ví dụ mã bên dưới, tôi hiển thị cảnh báo trình phân tích mã trong nhận xét. Để đơn giản hóa mã Tôi không thực sự thực hiện bất kỳ cuộc gọi đến HtmlTextWriter (bạn sẽ thấy một bình luận cho hiệu ứng đó trong mỗi chức năng). Làm thế nào tôi có thể viết mã đúng cách để tránh các cảnh báo?Cách sử dụng StringWriter và HtmlWriter cùng nhau mà không có cảnh báo Phân tích Mã số
// 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;
}
rất nhiều ý kiến thú vị về [CA2202] (http://msdn.microsoft.com/en-us/library/ms182334.aspx) trên MSDN –
Đáng tiếc là không ai trong số các ý kiến tại Liên kết CA2202 ở trên đã giải quyết cảnh báo với cặp đặc biệt này khi tôi thử chúng. –
Trong [một bài đăng khác] (http://stackoverflow.com/questions/3831676/ca2202-how-to-solve-this-case), Hans Peter viết "Đây không phải là các lỗi thực sự chính xác, các lớp .NET này có khả năng phục hồi nhiều cuộc gọi Dispose(). " Với ý nghĩ đó, tôi bị cám dỗ để làm việc sử dụng hai lần như Func3 hoặc Func4 và ngăn chặn cảnh báo CA2202. –