嵌套使用语句的最佳实践?
我有一个代码块如下,我使用3嵌套using
块。
我发现使用try finally
块我可以避免这种情况,但如果有两个以上的使用语句,那么最好的方法是什么?
private FileStream fileStream = null; private Document document = null; private PdfWriter pdfWriter = null; using (fileStream = new FileStream("ABC.pdf", FileMode.Create)) { using (document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) { using (pdfWriter = PdfWriter.GetInstance(document, fileStream)) { document.AddAuthor(metaInformation["author"]); document.AddCreator(metaInformation["creator"]); document.AddKeywords("Report Generation using I Text"); document.AddSubject("Document subject"); document.AddTitle("The document title"); } } }
您可以通过以下方式删除嵌套缩进:
using (var fileStream = new FileStream("ABC.pdf", FileMode.Create)) using (var document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) using (var pdfWriter = PdfWriter.GetInstance(document, fileStream)) { // code }
避免缩进的一点点冗长的方法:
using (var fileStream = new FileStream("ABC.pdf", FileMode.Create)) using (var document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) using (var pdfWriter = PdfWriter.GetInstance(document, fileStream)) { document.AddAuthor(metaInformation["author"]); document.AddCreator(metaInformation["creator"]); document.AddKeywords("Report Generation using I Text"); document.AddSubject("Document subject - Describing the steps creating a PDF document"); document.AddTitle("The document title - PDF creation using iTextSharp"); }
正如Jon Skeet指出的那样,这些变量不需要是实例变量,因为它们无论如何都要在using
块之后处理。
您可以使用上面代码中显示的局部变量。
也许是传统的东西 在我看来 ,选择两者之间的最佳方法是;
-
Using
:如果要在上下文中使用实例,则需要在完成后对其进行处理 -
try/finally
:如果您期待任何问题并且与exception有关,请在Dispose
您正在使用的实例之前捕获它。
正如其他评论/答案所述; 你不需要实例级变量;
using (FileStream fileStream = new FileStream("ABC.pdf", FileMode.Create)) using (Document document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream)) { // # Implementation here seems like a good approach }
在单一方法中,您不需要处理或更改数据; Jan Sommer建议的选项将是我的选择。 但是,在某些情况下,DisposableList很有用。 特别是,如果你有许多一次性领域都需要处理(在这种情况下,你不能使用)。
需要您记住将项目添加到列表中。 (虽然您也可以说您必须记住使用。)如果其中一个处置方法抛出,则中止处理过程,而剩余的项目不会被丢弃。
public class DisposableList : List, IDisposable { public void Dispose() { if (this.Count > 0) { List exceptions = new List (); foreach (var disposable in this) { try { disposable.Dispose(); } catch (Exception e) { exceptions.Add(e); } } base.Clear(); if (exceptions.Count > 0) throw new AggregateException(exceptions); } } public T Add(Func factory) where T : IDisposable { var item = factory(); base.Add(item); return item; } }
现在捕获Dispose调用中的任何exception,并在遍历所有项目后抛出新的AggregateException。 我添加了一个帮助器添加方法,允许更简单的使用:
using (var disposables = new DisposableList()) { var file = disposables.Add(() => File.Create("test")); // ... var memory = disposables.Add(() => new MemoryStream()); // ... var cts = disposables.Add(() => new CancellationTokenSource()); // ... }