18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

.NET APPLICATION PROGRAMMING INTERFACE<br />

.NET runtime wraps the COM object reference in a generic RCW called<br />

System.__ComObject. This is a class internal to the .NET Framework that can be<br />

used to hold a reference to any kind of COM object; its purpose is to act as the<br />

RCW for an unknown Type of COM object.<br />

Casting<br />

Looking again at the second example, even if you actually know the exact Type<br />

of class to which you have a reference, the .NET runtime still does not have the<br />

metadata required to cast the variable to a strongly typed RCW; this can be seen<br />

in the following code, as attempting a cast to the SimpleMarkerSymbolClass Type<br />

would fail.<br />

[C#]<br />

// The following line would result in sym2 being null as the cast would<br />

fail.<br />

ESRI.<strong>ArcGIS</strong>.Display.SimpleMarkerSymbolClass sym2 = sym as<br />

ESRI.<strong>ArcGIS</strong>.Display.SimpleMarkerSymbolClass;<br />

[Visual Basic .NET]<br />

' The following line would result in a runtime error as the implicit cast<br />

would fail.<br />

Dim sym2 As ESRI.<strong>ArcGIS</strong>.Display.SimpleMarkerSymbol = sym<br />

However, as the System.__ComObject class is specifically designed to work with<br />

COM objects, it is always able to perform a QI to any COM interfaces that are<br />

implemented by an object. Therefore, casting to specific interfaces (as long as they<br />

are implemented on the object) will be successful.<br />

[C#]<br />

ESRI.<strong>ArcGIS</strong>.Display.ISimpleMarkerSymbol sym3 = sym as<br />

ESRI.<strong>ArcGIS</strong>.Display.ISimpleMarkerSymbol;<br />

[Visual Basic .NET]<br />

Dim sym3 As ESRI.<strong>ArcGIS</strong>.Display.ISimpleMarkerSymbol = sym<br />

Singletons and System.__ComObject<br />

In the examples above, a strongly typed RCW is created when you instantiate the<br />

COM object by using the ‘new’ keyword, whereas if the object is preexisting, the<br />

Type of the RCW is the generic System.__ComObject. Sometimes when you use<br />

the ‘new’ keyword to instantiate a COM object, you are actually getting a reference<br />

to an object that already exists—this happens when you attempt to instantiate<br />

a singleton class that has previously been instantiated. The .NET framework is<br />

unable to wrap in a strongly typed RCW an instance of an object that has previously<br />

been wrapped in the generic System.__ComObject RCW. If your code has<br />

encountered such a situation, you may receive an error such as<br />

‘Unable to cast object of type System.__ComObject to type ’.<br />

[C#]<br />

ESRI.<strong>ArcGIS</strong>.Display.IStyleGallery sg = new<br />

ESRI.<strong>ArcGIS</strong>.Framework.StyleGalleryClass();<br />

[Visual Basic .NET]<br />

Dim sg As ESRI.<strong>ArcGIS</strong>.Display.IStyleGallery = = New<br />

ESRI.<strong>ArcGIS</strong>.Framework.StyleGalleryClass<br />

This error may occur even though you have declared your variable using the<br />

interface name rather than the class name, as shown above. The problem occurs<br />

Chapter 4 • <strong>Developer</strong> environments • 159

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

Saved successfully!

Ooh no, something went wrong!