Reputation: 7908
I have a disposable class which implements IDisposalbe interface with disposable pattern. To maintain the constrains, I need to make sure that when the object has been disposed, any subsequent call to any public method throws ObjectDisposedException. The obvious solution is to check a bool value isDisposed and throw an exception. But how can I use some reflection based solution (if any or some other approach) to automatically apply this constrain to every public method. What I don’t want to do is to do this check in every method I have (i.e. prior to every call I do NOT want to call IsObjectDisposed() method)
Apart from the practical application as I have described earlier I need to know a way to automatically call a method let’s say Foo() for every call of a method of a class say MyClass
Upvotes: 1
Views: 1151
Reputation:
Have you thought about simply performing a null check prior to using the code?
ie. if(null!=object) { do operations }
Or are you looking for some kind of object wrapper instead using the virtual keyword? ie use Aspects using unity for example or spring. These allow you to Dependency Inject code into your application and allow you to apply "cross cutting concerns" through your code base ie security, logging, printing and boundary checking.
Upvotes: 0
Reputation: 62266
Imo, you have 2 solutions here:
or call them manually, so code actually, that for every public
method the check is executed.
or can use Aspect Oriented Programming to inject the function call into the begining of every method marked by some attribute.
Upvotes: 2