I'm wondering if I'm instantiating a new class with the IGeoProcessor2
object correctly. It's working the way I have it now, but I feel like there's something else I should be doing so I don't have to turn "Embed Interop Types" in the Properties window to False. I'm working with ArcObjects 10.5, ArcGIS 10.5, and Visual Studio 2015.
This is the code I have to instantiate a new geoprocessing object:
IGeoProcessor2 gp = new GeoProcessorClass();
If I leave the Esri.ArcGIS.Geoprocessing assembly's "Embed Interop Types" property on "True", I get a squiggly red line under GeoProcessorClass
. If I set it to "False", the squiggly goes away and everything works as expected. The same thing also happens with the ESRI.ArcGIS.System assembly when I use this code:
IVariantArray parameters = new VarArrayClass();
If I were to set the "Embed Interop Types" to false for the System assembly then the squiggly under VarArrayClass
goes away and everything works great.
Is this something everyone has to do to get their code to work, or am I missing something that allows me to keep "Embed Interop Types" as "True"?
I am using IGeoProcessor2
instead of GeoProcessor
because I am running a custom tool out of a custom toolbox, so I cannot switch to GeoProcessor
. For whatever reason, I cannot get it to work with my code.
1 Answer 1
If 'Embed Interop Types' is set to true then omit the 'Class' at the end of the type.
IVariantArray parameters = new VarArray();
IGeoProcessor2 gp = new GeoProcessor() as IGeoProcessor2;
-
Hm, when I do, I get "cannot implicitly convert type 'ESRI.ArcGIS.Geoprocessing.GeoProcessor' to ESRI.ArcGIS.Geoprocessing.IGeoProcessor2'. An explicit conversion exists (are you missing a cast?)" warning. This only occurs for the GeoProcessor, the VarArray does work when I remove the class.MKF– MKF2018年01月31日 15:21:06 +00:00Commented Jan 31, 2018 at 15:21
-
You just need to cast the new object to IGeoProcessor2. Some objects have implicit conversions others do not.danielm– danielm2018年01月31日 15:25:43 +00:00Commented Jan 31, 2018 at 15:25
-
Thanks, I'm still new at this.
IGeoProcessor2 gp = (IGeoProcessor2) new GeoProcessor();
worked. (I just saw you updated your answer, the "as" looks like a much cleaner way to type it. Great!)MKF– MKF2018年01月31日 15:27:06 +00:00Commented Jan 31, 2018 at 15:27