Getting the real COM class name

1001
5
07-17-2012 11:15 PM
JensHaffner
New Contributor II
This problem really puzzles me...
I???d like to get the ArcObjects CoClass name behind the interface pointer, which I have.
If I???ve got an IFeature pointer, I???d like to know whether it???s in fact a Feature object or perhaps a ComplexEdgeFeature or something else.
No matter what I try, I don???t get the right name???
0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
Have you tried something like this (in VB.NET)

If TypeOf IFeature Is IComplexEdgeFeature Then
  
End If
0 Kudos
NeilClemmons
Regular Contributor III
As Ken mentioned you can usually use TypeOf and test for a particular interface that will identify the underlying class.  In .NET you can also use the GetType method and check the FullName property.  This will give you the fully qualified class name.

Dim layer As IFeatureLayer = New FeatureLayer
                Dim s As String = layer.GetType.FullName
0 Kudos
JensHaffner
New Contributor II
Ken, you are absolutely right, it is possible to check it this way:
If TypeOf IFeature Is IComplexEdgeFeature Then
End If

But I've learned, that in some cases it's difficult to pick an interface that is unique for a particular COM class.
0 Kudos
JensHaffner
New Contributor II
Neil, perhaps I'm doing somthing wrong here, cause I've tried this:
IWorkspace pWorkspace = EsriDatabase.FileGdbWorkspaceFromPath("WhatEver.gdb");
Console.WriteLine(pWorkspace.GetType().FullName);

This code outputs "System.__ComObject" no matter which interface or class I use.
0 Kudos
sapnas
by
Occasional Contributor III
its a little tricky to get the type of com object. you may have to iterate through all the types and compare the object pointer with array of pointers returned by the com object. Below is the code to accomplish this. Hope this helps

 IntPtr iunkwn = Marshal.GetIUnknownForObject(pWorkspace);
            // enum all the types defined in the interop assembly
            System.Reflection.Assembly objAssembly =
            System.Reflection.Assembly.GetAssembly(typeof(IWorkspace));
            Type[] objTypes = objAssembly.GetTypes();
            // find the first implemented interop type
            foreach (Type currType in objTypes)
            {
                // get the iid of the current type
                Guid iid = currType.GUID;
                if (!currType.IsInterface || iid == Guid.Empty)
                {
                    // com interop type must be an interface with valid iid
                    continue;
                }
                // query supportability of current interface on object
                IntPtr ipointer = IntPtr.Zero;
                Marshal.QueryInterface(iunkwn, ref iid, out ipointer);
                if (ipointer != IntPtr.Zero)
                {
                    string str =currType.FullName;
                }
            }

0 Kudos