Sometimes I need to use many disposable objects within the function. The most common case is the stream-reader and stream The driver is getting, but sometimes it is even more.
Using nested words quickly add nested and look ugly. To solve this I have created a small square that collects idispositional objects and removes them when they are disposed of themselves.
Public class multidapz: hashaseet & lt; IDisposable & gt; IDisposable {PUBLIC MultiDispose (Paramayis IDisposable [] Objects Tudoods) {foreach (IDisposable in Objects Tudes) {This.ed (D); }} Add public te & lt; T & gt; (T-OBJ) where T: IDisposable {base.Add (obj); Return obj; } Public Zero DisposeObject (IDisposable obj) {obj.Dispose (); Base.Remove (obj); } #region IDisposable Members Public Zero () {foreach (IDisposable D in this) {d.Dispose (); So, my code looks like this now: using (multi-functional MD = new multidose ()) {StreamReader rdr = Add md (new stream reader (args [0])); StreamWriter wrt = md.Add (new stream vaitters (args [1])); In any case, alesz disposing w = MD. Add (New JECLEIDS Disposing ()); // code}
Is there something wrong with this approach that can cause problems down the road? I left the work taken from Hashat for purpose, so that the classroom is more flexible. Of course, due to misuse of this function the object can not be properly disposed, but again there are many other ways to shoot himself in this leg without the square.
You can do this:
(var a = new Using A ()) (var b = new B ()) /// ..}
Comments
Post a Comment