Click here to Skip to main content
13,250,029 members (66,112 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 17 Feb 2009

Get all the types a COM object implements

, 17 Feb 2009
Rate this:
Please Sign up or sign in to vote.
How to return all types that a COM object implements.


This article shows how to get all the types a COM object implements.


When you get a COM object in C#, if you know the classes and the interfaces it implements, you can easily cast it to the desired type. But, what if you don't know its type. Or more to the point, you think you know it's type and it doesn't implement that type.

Using the code

The following method will return all the types that a COM object implements. I don't use this in production code, but I do use it occasionally when writing code.

/// <summary>
/// Get all implemented types in a COM object. Code based on work at
///          the-type-of-a-com-object-system__comobject-with-visual-c-net/
/// </summary>
/// <param name="comObject">The object we want all types of.</param>
/// <param name="assType">Any type in the COM assembly.</param>
/// <returns>All implemented classes/interfaces.</returns>
public static Type[] GetAllTypes(object comObject, Type assType)

    // get the com object and fetch its IUnknown
    IntPtr iunkwn = Marshal.GetIUnknownForObject(comObject);

    // enum all the types defined in the interop assembly
    Assembly interopAss = Assembly.GetAssembly(assType);
    Type[] excelTypes = interopAss.GetTypes();

    // find all types it implements
    ArrayList implTypes = new ArrayList();
    foreach (Type currType in excelTypes)
        // com interop type must be an interface with valid iid
        Guid iid = currType.GUID;
        if (!currType.IsInterface || iid == Guid.Empty)

        // query supportability of current interface on object
        IntPtr ipointer;
        Marshal.QueryInterface(iunkwn, ref iid, out ipointer);

        if (ipointer != IntPtr.Zero)


    // no implemented type found
    return (Type[])implTypes.ToArray(typeof(Type));


Original post.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

David Thielen
Chief Technology Officer Windward Studios
United States United States
CTO/founder - Windward Studios

You may also be interested in...

Comments and Discussions

GeneralCouldn't you... Pin
Qwertie28-Feb-09 5:38
memberQwertie28-Feb-09 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 17 Feb 2009
Article Copyright 2009 by David Thielen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid