ReleaseComObject and IsComObject
We had a COM Object that needed to have ReleaseComObject called on it. This worked fine, and happily for a while. Then, someone created a .NET object with the exact same signature so that it might easily replace the use of the COM object. However, this new assemebly is NOT a COM Object, so it's extraordinarily bad to call ReleaseComObject on it (in that it totally doesn't work.)
So, here's a good best practice if you're doing some crazy crap like this:
if(Marshal.IsComObject(foo)) Marshal.ReleaseComObject(foo);
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.



About Newsletter
Do we really want to know via an exception that the operation is meaningless?
--
Thomas
Comments are closed.
That all works unless your client code calls ReleaseComObject without checking IsComObject first. Since it isn't a COM object anymore you will get an InvalidCastException. Like Scott said, it's a good practice as it allows you to convert your legacy COM objects to managed code in the future with no client side code change.