ArcObject必须使用反射创建实例对象

在ArcObject开发时,有时非常让人无语的是使用new创建对象时,报"无法将类型为Systerm._ComObject的对象强制转换为类型******",官方知道这是个坑,早就讲得非常清楚和透彻了。

Interacting with singleton objects (ArcObjects .NET 10 SDK) (arcgis.com)

大体原因:当使用New关键字创建实例化COM对象时,默认会创建一个强类型的RCW,如果这个对象已存在,则会创建为弱类型的RCW,即System.__ComObject,而.NET Framework 无法将new的强类型RCW接口转换为单实例弱类型RCW。

错误写法:

ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem(4490);

正确写法:

Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
object obj = Activator.CreateInstance(t);
ISpatialReferenceFactory spatialReferenceFactory = obj as ISpatialReferenceFactory;
ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem(4490);

那么,不禁要问了,ArcObject中哪些对象是单实例呢?下面根据官方的文档整理了一下:

ProgID

备注

AccessWorkspaceFactory

esriDataSourcesGDB.AccessWorkspaceFactory

常用

ActionEnvironment*

esriTrackingAnalyst.ActionEnvironment

 

AGAnimationUtils

esriAnimation.AGAnimationUtils

 

AGSServerConnectionFactory

esriGISClient.AGSServerConnectionFactory

 

AMSWorkspaceFactory

esriTrackingAnalyst.AMSWorkspaceFactory

 

AnimationControlsDialog

esriAnimationUI.AnimationControlsDialog

仅Desktop

AnimationCreateKeyframeDialog

esriAnimationUI.AnimationCreateKeyframeDialog

仅Desktop

AnimationFromPathDialog

esriAnimationUI.AnimationFromPathDialog

仅Desktop

AnimationLayerGroupDialog

esriAnimationUI.AnimationLayerGroupDialog

仅Desktop

AnimationManagerDialog

esriAnimationUI.AnimationManagerDialog

仅Desktop

AnimationTypeCamera

esri3DAnalyst.AnimationTypeCamera

 

AnimationTypeGlobeCamera

esriGlobeCore.AnimationTypeGlobeCamera

 

AnimationTypeGlobeLayer

esriGlobeCore.AnimationTypeGlobeLayer

 

AnimationTypeLayer

esri3DAnalyst.AnimationTypeLayer

 

AnimationTypeMapLayer

esriAnimation.AnimationTypeMapLayer

 

AnimationTypeMapView

esriAnimation.AnimationTypeMapView

 

AnimationTypeScene

esri3DAnalyst.AnimationTypeScene

 

AnimationTypeTimeLayer

esriAnimation.AnimationTypeTimeLayer

 

AppRef*

esriFramework.AppRef

仅Desktop

ArcInfoWorkspaceFactory

esriDataSourcesFile.ArcInfoWorkspaceFactory

 

CadWorkspaceFactory

esriDataSourcesFile.CadWorkspaceFactory

常用

CieLabConversion

esriDisplay.CieLabConversion

 

CommandsEnvironment

esriControls.CommandsEnvironment

 

DddEnvironment

esri3DAnalystUI.DddEnvironment

仅Desktop

DddServerEnvironment

esriGeodatabase.DddServerEnvironment

 

DDDToolbarEnvironment

esri3DAnalystUI.DDDToolbarEnvironment

仅Desktop

DEGdbUtilities

esriGeodatabase.DEGdbUtilities

 

DEUtilities

esriGeoprocessing.DEUtilities

 

DummyGraduatedMarkerLayer

esriCarto.DummyGraduatedMarkerLayer

 

DummyLayer

esriCarto.DummyLayer

 

EngineEditor

esriControls.EngineEditor

常用,仅Engine

EngineNetworkAnalystEnvironment

esriControls.EngineNetworkAnalystEnvironment

Engine

EnvironmentManager

esriSystem.EnvironmentManager

常用

ErrorDialog

esriSystemUtility.ErrorDialog

Engine (C++)

ErrorMessage

esriSystemUtility.ErrorMessage

Engine (C++)

ExcelWorkspaceFactory

esriDataSourcesOleDB.ExcelWorkspaceFactory

常用

ExtensionManager

esriSystem.ExtensionManager

 

FileGDBWorkspaceFactory

esriDataSourcesGDB.FileGDBWorkspaceFactory

常用

FindDialog

esriCatalogUI.FindDialog

仅Desktop

FormatList

esriDataSourcesRaster.FormatList

 

GeoDatabaseHelper

esriGeodatabase.GeoDatabaseHelper

 

GMxEffectsToolbarEnvironment

esriArcGlobe.GMxEffectsToolbarEnvironment

仅Desktop

GPGlobeFunctionFactory

esriGlobeCore.GPGlobeFunctionFactory

 

GPHolder

esriGeoprocessing.GPHolder

 

GPSAUtil

esriSpatialAnalyst.GPSAUtil

 

GPServerFunctionFactory

esriGeoprocessing.GPServerFunctionFactory

 

GpsPositionDialog

esriArcMapUI.GpsPositionDialog

仅Desktop

GPUtilities

esriGeoprocessing.GPUtilities

 

GxCatalogROT

esriCatalogUI.GxCatalogROT

仅Desktop

GxDatabaseExtensions

esriCatalog.GxDatabaseExtensions

仅Desktop

IdentifyDialog

esriCartoUI.IdentifyDialog

仅Desktop

IMSWorkspaceFactory

esriGISClient.IMSWorkspaceFactory

 

InMemoryWorkspaceFactory

esriDataSourcesGDB.InMemoryWorkspaceFactory

 

LabelEnvironment

esriCarto.LabelEnvironment

 

LayerEffectsEnvironment

esriArcMapUI.LayerEffectsEnvironment

仅Desktop

LocatorManager

esriLocation.LocatorManager

 

MemoryRelationshipClassFactory

esriGeodatabase.MemoryRelationshipClassFactory

 

MessageLogger

esriSystem.MessageLogger

 

MetadataSynchronizer

esriGeodatabase.MetadataSynchronizer

 

MonitorSettings

esriDisplay.MonitorSettings

 

MyPlaceCollection

esriControls.MyPlaceCollection

Engine

NADirectionsWindowManager

esriNetworkAnalystUI.NADirectionsWindowManager

仅Desktop

NetCDFWorkspaceFactory

esriDataSourcesNetCDF.NetCDFWorkspaceFactory

 

NetworkIdentifyDialog

esriNetworkAnalystUI.NetworkIdentifyDialog

仅Desktop

OLEDBWorkspaceFactory

esriDataSourcesOleDB.OLEDBWorkspaceFactory

常用

PCCoverageWorkspaceFactory

esriDataSourcesFile.PCCoverageWorkspaceFactory

 

RasterAnalysisToolbarEnvironment

esriSpatialAnalystUI.RasterAnalysisToolbarEnvironment

仅Desktop

RasterAttributeTableManager

esriDataSourcesRaster.RasterAttributeTableManager

 

RasterDefaultsEnv

esriCarto.RasterDefaultsEnv

 

RasterSettings

esriSpatialAnalystUI.RasterSettings

仅Desktop

RasterStatus

esriDataSourcesRaster.RasterStatus

 

RasterWorkspaceFactory

esriDataSourcesRaster.RasterWorkspaceFactory

常用

RelQueryTableFactory

esriGeodatabase.RelQueryTableFactory

常用

SAExtension

esriSpatialAnalystUI.SAExtension

仅Desktop

SchematicProjectMgr

esriSchematic.SchematicProjectMgr

 

SchematicWorkspaceFactory

esriSchematic.SchematicWorkspaceFactory

 

ScratchWorkspaceFactory

esriDataSourcesGDB.ScratchWorkspaceFactory

 

SDCWorkspaceFactory

esriDataSourcesFile.SDCWorkspaceFactory

 

SdeWorkspaceFactory

esriDataSourcesGDB.SdeWorkspaceFactory

常用

SelectionEnvironment

esriCarto.SelectionEnvironment

 

ServerStyleGallery

esriDisplay.ServerStyleGallery

 

ShapefileWorkspaceFactory

esriDataSourcesFile.ShapefileWorkspaceFactory

常用

SpatialReferenceEnvironment

esriGeometry.SpatialReferenceEnvironment

常用

StreetMapWorkspaceFactory

esriDataSourcesFile.StreetMapWorkspaceFactory

 

StyleGallery

esriFramework.StyleGallery

仅Desktop

SxAnimationEnvironment

esri3DAnalyst.SxAnimationEnvironment

 

SxEffectsToolbarEnvironment

esriArcScene.SxEffectsToolbarEnvironment

仅Desktop

SymbologyEnvironment

esriDisplay.SymbologyEnvironment

 

SystemHelper

esriSystem.SystemHelper

 

TAExtension

esriTrackingAnalystUI.TAExtension

仅Desktop

TextFileWorkspaceFactory

esriDataSourcesOleDB.TextFileWorkspaceFactory

常用

TexturePersistenceProperties

esriGeometry.TexturePersistenceProperties

 

TinWorkspaceFactory

esriDataSourcesFile.TinWorkspaceFactory

常用

TMS_MarshalHelper

esriTrackingAnalyst.TMS_MarshalHelper

 

ToolboxWorkspaceFactory

esriGeoprocessing.ToolboxWorkspaceFactory

 

TrackingEnvironment

esriTrackingAnalyst.TrackingEnvironment

 

VpfWorkspaceFactory

esriDataSourcesFile.VpfWorkspaceFactory

 

WMSConnectionFactory

esriGISClient.WMSConnectionFactory

 

WorkspaceFactory

esriGeodatabase.WorkspaceFactory

 

XMLTypeMapper

esriSystem.XMLTypeMapper

 
posted @ 2023-01-10 08:14  我也是个傻瓜  阅读(145)  评论(0编辑  收藏  举报