15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using a CoM Component from a .neT Client ❘ 709<br />

Earlier you learned that the COM object is released as soon as the reference count is 0.<br />

Marshal.ReleaseComObject() decrements the reference count by 1 by invoking the<br />

Release() method. Because the RCW does just one call to AddRef() to increment<br />

the reference count, a single call to Marshal.ReleaseComObject() is enough to release<br />

the object no matter how many references to the RCW you keep.<br />

After releasing the COM object using Marshal.ReleaseComObject() , you may not use any variable that<br />

references the object. In the example, the COM object is released by using the variable math . The variable<br />

welcome , which references the same object, cannot be used after releasing the object. Otherwise, you will<br />

get an exception of type InvalidComObjectException .<br />

Releasing COM objects when they are no longer needed is extremely important. COM<br />

objects make use of the native memory heap, whereas .<strong>NET</strong> objects make use of the<br />

managed memory heap. The garbage collector only deals with managed memory.<br />

As you can see, with a runtime callable wrapper, a COM component can be used similarly to a .<strong>NET</strong> object.<br />

using the Com server with dynamic language extensions<br />

<strong>C#</strong> 4 includes an extension for using dynamic languages from <strong>C#</strong>. This is also an advantage for using COM<br />

servers that offer the IDispatch interface. As you read earlier in the “ Dispatch Interfaces ” section, this<br />

interface is resolved at runtime with the methods GetIdsOfNames() <strong>and</strong> Invoke() . With the dynamic<br />

keyword <strong>and</strong> the help of a COM binder that is used behind the scenes, the COM component can be called<br />

without creating an RCW object.<br />

Declaring a variable of type dynamic <strong>and</strong> assigning a COM object to it uses the COM binder, <strong>and</strong> you can<br />

invoke the methods of the default interface as shown. Creating an instance of the COM object without using<br />

an RCW can be done by getting the Type object using Type.GetTypeFromProgID() , <strong>and</strong> instantiating<br />

the COM object with the Activator.CreateInstance() method. You just don ’ t get IntelliSense with the<br />

dynamic keyword, but can use the optional parameters that are very common with COM:<br />

using System;<br />

namespace Wrox.ProCSharp.Interop<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

Type t = Type.GetTypeFromProgID("COMServer.COMDemo");<br />

dynamic o = Activator.CreateInstance(t);<br />

Console.WriteLine(o.Greeting("Angela"));<br />

}<br />

}<br />

}<br />

code snippet DynamicDotnetClient/Program.cs<br />

The dynamic language extensions of <strong>C#</strong> are explained in Chapter 12, “Dynamic<br />

Language Extensions.”<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!