Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Trying to check the type of some Elements if they are (ITextElement , MarkerElement), when i try .getType() function it returns always "System._COM_object".

How i could get the type of an object if i don't know its type before ? because i could only detect the types that i pretend using (obj as Interface) != null.

but for the types that i don't pretend i get always null from (obj as Interface) .

Any help please.

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

In ArcObjects, like other COM-based libraries, interface testing is the main way you determine the type (using that term loosely) of object you have:

if (element is ITextElement):
    // Do something
else if (element is IMarkerElement):
    // Do something else
etc.

Another thing you can do is, if the object implements IPersist, you can get its CLSID using GetClassID, and, from there (if desired), you can get its ProgID using the WinAPI ProgIDFromCLSID function. This is useful for cases where the object has no other indicative interfaces.

Lastly, and I've never used this myself as it seems very unwieldy and potentially very slow, is to use reflection to query interface the object against every interface in an assembly to see which interfaces it supports:

    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;
        }
    }

(Source: Re: Getting the real COM class name)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.