dotNet 'isDisposable'

Started by galaara98, October 21, 2016, 10:49:43 AM

Previous topic - Next topic


i am trying to find a way to 'thouroughly' manually garbage dispose of CLR objects when i use them.
In many .Net centric programming languages now you have the concept of a "using block", and once you leave the "using block" (like leaving a local function) all objects that can be disposed are disposed.

So i don't have a using mechanism, but i would like to create a mechanism (for now, lets call it a global subroutine)
that i can send each CLR object to...that asks
can this object be disposed, and if so call it's disposed method

I was reading an article on

If a type implements the IDisposable interface, you should always call the Dispose method on an instance of the class when you are done using it.  The presence of IDisposable indicates that the class has some resources that can be released prior to garbage collection.
Below is one pattern for checking to see if an object implements the IDisposable interface.

Dog bob = new Dog("Bob", 5);
if (bob is IDisposable) ((IDisposable)bob).Dispose();

so, in winbatch CLR integration, assuming i have a variable that is a CLR object
(lets say i set ObjSearcher = ObjectCLRNew( 'System.DirectoryServices.DirectorySearcher')
and i use it, i'm done with it, etc.

In winbatch how can i programically know if objSearcher "implements the IDisposable interface", and if that is way to complicated.. then can I "query" if it has a .Dispose() method?


You could simply just read the object documentation or if you think it's really worth it, use reflection.   That is the kind of 'problem' reflection is intended to solve.  Interestingly enough, if you spend a lot of time staring at the source code for MSFT's .Net Framework, you will find that the 'Dispose' method sometimes (haven't counted the percentages) does very little.     
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade


yeah, i currently read the docs on each object.. my hope was more or less to attempt dispose if available on any CLR object so i could just throw mud at it.  the reason this is handy is alot of times i create an object with intent, but its methods return different object types, or properties sometimes are objects too (so when i 'get' a property, it return a whole new object type)

so it is 'lazier'... but is also for simplifies and streamlining my coding process.

as a note, this all comes from certain .net objects that seem be .NET wrappers for com objects, that often are wrappers for other com objects...
anyway i have found from personal experience that when i have a local function in WB, that creates "new" com or clr objects, its just more stable to clean them up as best as I can before I leave the scope.  I know a lot of programmers don't implement dispose, or when they do it's often a lie, and it doesn't do anything useful.. but i have seen improved stability by trying to dispose as often as possible.


ok... i use .NET almost exclusively from Powershell and WB... and so i get the liberty of only understanding .NET at a certain depth
(because both of those languages are so flexible at interpreting what your probably meant)

so please forgive my naïveness.. i dont know how to use reflection to get this information.
i did call the .GetType().GetMethods() on the object, but it returned an single dimensional array: of objects, and i started wondering if i care enough to process them :)


MSFT makes it relatively easy to explore class hierarchies (included noting methods inherited from base classes)  and of class object returned by methods and properties of classes.  It has been my experience that one of the best techniques for creating clean and stable software is to read the documentation for the stuff you are using.

Saying something nice about MSFT documentation is bit unusual but they do get it more or less close to right occasionally...

If you want to familiarize yourself with reflection, a Web search might be the best starting point.  It is an often covered topic on many c#/.Net coding sites.  A very simplistic start would look something like the following using WinBatch CLR hosting:
Code (winbatch) Select
objType = objWhatever.GetType()
aMemberInfo = objType.GetMember("Dispose")
if ArrInfo(aMemberInfo, 1) then strHasDispose = "Yes"
else strHasDispose = "No"

Message("Can Dispose?", strHasDispose )

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade