lundi 24 août 2015

how to write an If statement that has some disposable objects in it

I have following piece of C# code

  iSomeObject.MyPoint(SomeConstants.cpU, 2,
                          myInterface.MySystem.MyCustomType == OtherConstants.cpProjected ? Constants.cpU : Constants.cpT,
                          1, ref t1, ref t2, ref t3);

When I ran my proprietary analysis tool it says that myInterface.MySystem is causing a resource leak.

 class MyClass:MyInterface,IDisposable
 {}
  MyInterface myInterface = new MyClass();

And I'm explicitly disposing off instance myInstance by casting it back to IDisposable and calling Dipose() method on it.

Now MySystem properties' get method is invoked on myInterface which(MySystem) is of type IExampleInterface which is further implemented like the following code:

 class ExampleClass:IExampleClass,IDisposable
 {}

I'm not sure whether calling a Dipose() method on myInterface will also dispose off resource created by MySystem or do I need to explicitly call Dispose() method on it as well. But all is happening inside IF statement condition and I'm not sure how to handle this situation and make my code dispose off all disposable objects in other words I'm not sure about the C# syntax in this case and how to deal with dispoal concept in such a scenario.

Any suggestion will be appreciated.

Aucun commentaire:

Enregistrer un commentaire