IDisposable and Marshal.ReleaseCOMObject and dynamic in C#

 

If you have worked with COM, you know that for every COM object that you access is not only necessary to null the reference, but also call Marshall.ReleaseCOMObject .

This is a very error prone task, because you have all try / finally blocks

 
// declare a, ws, w 
try{ 
a=new Application(); 
ws = a.Workbooks; 
w = ws.Add(); 
//code 
}
 catch(...) {
 if( w != null){ 
Marshal.ReleaseComObject(w); 
w = null; 
}
 if(ws !=null){ 
Marshal.ReleaseComObject(ws); 
ws = null; 
} 
if( a!= null) { 
Marshal.ReleaseComObject(a); a= null; 
} 
} 

All of this could be done easy with IDisposable, if

1. IDisposable calls the Marshal.ReleaseCOMObject

2. Find a way to call methods / properties on the COM object from the class that implements IDisposable

 

I have done this and the result is like this:

 
using (dynamic a = new ComDisposable(new Application())) { 
using (dynamic ws = a.Workbooks) { 
using (dynamic w = ws.Add()) { 
using (var shs = w.Sheets) { 
using (var s = shs[1]) { 
//Worksheet a; 
using (var r = a.Range("A1")) { 
r.Value2 = "http://ignatandrei.github.io/ToolsAndUtilities/";; 
using (var f = r.Font) { 
f.Bold = true; 
}
 }
 }
 }
 w.SaveAs(fileName); 
w.Close(); 
} 
}
 a.Quit(); } 

 

Video at https://youtu.be/2qbAcSjL1gU

NuGet package at https://www.nuget.org/packages/ReleaseComObjectDisposable/

Smart Copy and Paste from StackOverflow–part 6 from 7

I have written this mini e-book in order to remember myself how to do it .

I will put each week a part from my book here in the blog. If you want , however, buy the book, I will do appreciate !

 

Congratulations!

If you reached this step, that means your code[1] is tested and performs well. That also means that you are now ready for the next challenge: find again a code to solve everyday problems.


[1]yes, this is now YOUR code!

 

 

 

The book link is http://www.amazon.com/Smart-Paste-Stack-Overflow-other-ebook/dp/B01EHI5RQM

Smart Copy and Paste from StackOverflow–part 5 from 7

I have written this mini e-book in order to remember myself how to do it .

I will put each week a part from my book here in the blog. If you want , however, buy the book, I will do appreciate !

 

Copying code into the program

If it does not compile at first attempt, see if you are missing some reference / namespaces / others.

If again does not compile, repeat previous step:" searching " and take next links. Do not bother to improve the code . Codes are everywhere – and, if it is not written well, you will discover another one that is easy to copy / paste.

Verifying code

This is the tricky part. And this is the part that you should always have , if you copy paste other people code. And never test in production[1]

Sorting

For sorting you can create a List<Person> with some real names, taken from your colleagues / acquaintances . Then you will pass through the sorting function and see if it function.

Encrypting

For this you will create 2 functions: Encrypt and Decrypt. And ensure Encrypt(Decrypt( variable )) = variable. ( Again, you can use your name or any other site url)

If again this step does not work , see if you have caused some error when coding the test. If not, repeat step:" searching " and take next links .

if the code works as needed , I think that you should add some automatic test code.


[1] http://www.digitaljournal.com/technology/man-deletes-his-entire-company-with-a-single-line-of-code/article/462851

 

 

 

The book link is http://www.amazon.com/Smart-Paste-Stack-Overflow-other-ebook/dp/B01EHI5RQM