Search code examples
visual-studio-2010debuggingcomenvdte

How to tell type of indexing used from debugger


Following shows a C# console application stopped at a breakpoint. The sln variable is of type Solution2. From research, I determined that the Projects item in the solution uses 1-based indexing, so that's how I retrieve the only project in the Visual Studio solution (the line where the breakpoint is):

project = sln.Projects.Item(1);

What I was trying to do through the debugger was try to figure out if I could tell whether the collection was 0-based or 1-based, had I not had this information beforehand. But the debugger only shows that the Projects collection has a Count of 1. Is there a way (short of experimenting) to gain this knowledge by looking into the collection through the debugger?

Also, related questions:

What is the Dynamic View element?

Expanding the `[System.__ComObject] leads to a seemingly recursive display as below:

Why is this? What purpose does it serve?


Solution

  • To answer to your first question, there is no easy way to tell if COM based collection is 0 based or 1 based. Not, unless you are willing to disassemble the implementation of get_Item() method of the object that implements the COM interface. It could be either 0 or 1, and in general, it is not even guaranteed that indexes are expected to be integer values. In fact, the definition of your Projects.Item method takes System.Object as a parameter:

    Project Item(
        Object index
    )
    
    ---
    Parameters
        indexType: System.Object
    
        Required. The index of the item to return. 
    

    In your case, you can avoid using Item method, because Projects collection is IEnumerable, so you can just get the first element of the enumeration:

    #using System.Linq;
    ---
    var firstItem = sln.Projects.First();
    

    Your last question is just a bug (or "feature") of Visual Studio debugger. COM interop in VS debugger is not the best area. If you find you need to debug COM interop on lower lever, it is best to use low level debugger, like WinDbg and manually walk through interface vtable's.