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 />

because when your code instantiates an object, the .NET runtime first attempts<br />

to wrap the object in the strongly typed class Type (the Type stated after the new<br />

keyword) before attempting a cast to the interface type. The cast to the strongly<br />

typed RCW cannot succeed as the COM object has previously been wrapped in<br />

the generic System.__ComObject wrapper. This may occur in situations beyond<br />

your control. For example, other ArcObjects tools written in .NET from other<br />

third parties may wrap an object in the generic wrapper, causing your code to fail.<br />

The solution is to use the Activator class (as shown below) to safely wrap singleton<br />

objects in a strongly typed RCW when you first get a reference to them.<br />

Additionally, you should generally always declare variables holding RCWs using an<br />

interface rather than a class Type.<br />

Using the Activator class to create singletons<br />

If you use the CreateInstance method of the Activator class instead of the new<br />

keyword to instantiate singletons, you can avoid such errors, as the Activator is<br />

able to get the required metadata to perform the cast.<br />

[C#]<br />

Type t = Type.GetTypeFromProgID("esriFramework.StyleGallery");<br />

System.Object obj = Activator.CreateInstance(t);<br />

IStyleGallery sg = obj as IStyleGallery;<br />

[Visual Basic .NET]<br />

Dim t As Type = Type.GetTypeFromProgID("esriFramework.StyleGallery")<br />

Dim obj As System.Object = Activator.CreateInstance(t)<br />

Dim pApp As ESRI.<strong>ArcGIS</strong>.Display.IStyleGallery = obj<br />

You can use this technique to instantiate the AppRef class—remember, however,<br />

that the AppRef class can only be created within an <strong>ArcGIS</strong> application. (The<br />

Type is the generic System.__ComObject RCW.)<br />

[C#]<br />

Type t = Type.GetTypeFromProgID("esriFramework.AppRef");<br />

System.Object obj = Activator.CreateInstance(t);<br />

ESRI.<strong>ArcGIS</strong>.Framework.IApplication pApp = obj as<br />

ESRI.<strong>ArcGIS</strong>.Framework.IApplication;<br />

[Visual Basic .NET]<br />

Dim t As Type = Type.GetTypeFromProgID("esriFramework.AppRef")<br />

Dim obj As System.Object = Activator.CreateInstance(t)<br />

Dim pApp As ESRI.<strong>ArcGIS</strong>.Framework.IApplication = obj<br />

For more information about RCWs and interop, you may wish to refer to the<br />

book by Adam Nathan, .NET and COM—The Complete Interoperability <strong>Guide</strong>, Sams<br />

Publishing, 2002.<br />

Working with resources<br />

Using strings and embedded images directly (no localization)<br />

If your customization does not support localization now and you do not intend<br />

for it to support localization later, you can use strings and images directly without<br />

the need for resource files. For example, strings can be specified and used directly<br />

in your code:<br />

[VB.NET]<br />

160 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

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

Saved successfully!

Ooh no, something went wrong!