1

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?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89

1 Answers1

1

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.

seva titov
  • 11,720
  • 2
  • 35
  • 54
  • Thank you. Will start paying attention to the type of Item arguments, from now on. Is Linq access any slower than using `Item`? (Not that it would matter that much in this case.) Thanks. – Sabuncu Feb 28 '15 at 09:55
  • 1
    @Sabuncu, you are right, Linq will do it slower than direct `Item` method. Linq will first obtain an enumerator object, then start enumerating the object using its `Next` method, then it will have to destroy the enumerator object. For comparison, `Item` method simply returns pointer to existing object in collection, so it should be much faster. – seva titov Feb 28 '15 at 20:25
  • Thanks for responding in detail. – Sabuncu Mar 01 '15 at 06:57