How to tell type of indexing used from debugger - visual-studio-2010

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?

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.

Related

Visual Studio - Discover if method is called by another method at some point

I was wondering, does visual studio have a feature such that I gave it a 2 method names and it then works out if at somewhere along the call stack the first method is called by the second (statically, without having to debug).
e.g.
Say I have a method FireBullet, and I want to see if IsOutsideWestBoundary can be invoked at some point
FireBullet()->HitTest()->CheckBoundaries()->IsOutsiteWestBoundary()
You can see that FireBullet can eventually cause IsOutsideWestBoundary to be invoked at some point.
I understand this can potentially become a very large problem, especially with deep call stacks and multiple methods called at each level, but still, for relatively small call stacks depths, this could be very useful.
Surely something like this must exist?
Thanks
Thomas
The Visual Studio extension NDepend can do that. It lets write code rules and code queries through C# LINQ queries. The following LINQ code query, executed live in Visual Studio, answers your need:
from m in Methods
where m.Name == "FireBullet()"
let depth0 = m.DepthOfIsUsing("MyNamespace.Program.IsOutsiteWestBoundary()")
where depth0 >= 0 orderby depth0
select new { m, depth0 }
Notice that the code query result also provides the depth of call. It can be stored in your NDepend project, and it can be transformed into a rule by adding the prefix warnif count > 0.
This query gets inspired from the query generated by NDepend when right clicking a method and Select Methods ... that call me (directly or indirectly).
If you click the button Export to Graph you get such call graph (more info on this here):
A 14-day full featured trial is available here.
Disclaimer: I work for NDepend.

get contents of data grid view with Windows API

I am trying to see if there is a way to get the contents of a datagridview that is inside a top level window using Windows API's. I am using Visual Basic for this, but could also use C.
I doubt you could use the WinAPI to drill into a .NET app's DGV to get any meaningful information. But you might be able to using Reflection which enables you to obtain information about assemblies and the types defined within them, such as classes, interfaces, and value types. The ops are contained in the System.Reflection namespace.
Its fairly slow, so it is often a last resort, and the methods and such are pretty dense (convoluted) because you are getting at the information the 'long way around' by examining the Types and then asking the type for information.
This project shows how to access information in a different executing assembly. Given enough time and work and study, it should give you an idea how to do what you are asking.

Evaluating expressions using Visual Studio 2005 SDK rather than automation's Debugger::GetExpression

I'm looking into writing an addin (or package, if necessary) for Visual Studio 2005 that needs watch window type functionality -- evaluation of expressions and examination of the types. The automation facilities provide
Debugger::GetExpression, which is useful enough, but the information
provided is a bit crude.
From looking through the docs, it sounds like an
IDebugExpressionContext2 would be more useful. With one of these it
looks as if I can get more information from an expression -- detailed
information about the type and any members and so on and so forth, without having everything come through as strings.
I can't find any way of actually getting a IDebugExpressionContext2,
though! IDebugProgramProvider2 sort of looks relevant, in that I
could start with IDebugProgramProvider2::GetProviderProcessData and
then slowly drill down until reaching something that can supply my
expression context -- but I'll need to supply a port to this, and it's
not clear how to retrieve the port corresponding to the current debug
session. (Even if I tried every port, it's not obvious how to tell
which port is the right one...)
I'm becoming suspicious that this simply isn't a supported use case, but with any luck I've simply missed something crashingly obvious.
Can anybody help?
By using IDebugExpressionContext you'll ultamitely end up getting ahold of an instance of IDebugProperty. This interface is implemented by the Expression Evaluator service. This is, typically, a language specific service. It's designed to abstract out the language specific details of evaluating an expression. It understands much higher level commands like "Evaluate", and inspection.
I don't think you're going to get what you're looking for though because you can't get ahold of any kind of type object this way. Nearly all of the inspection methods return their results in String form. For example you won't get the type Int32 but instead the string "int". This makes type inspection next to impossible.
I don't believe what you're trying is a supported case. The type system being evaluated doesn't exist in the current process. It exists in the debuggee process and is fairly difficult to get access to.
There's a hack you could do to get more information about the type of a variable you've evaluated using Debugger::GetExpression method.
You could evaluate "AppDomain.CurrentDomain.GetAssemblies()" to get all the assemblies loaded into the debugee, and cache them in your add-in. You may also need to listen for new assemblies being loaded onto the AppDomain.
Then, run the following:
Expression myExpression = Debugger.GetExpression(...);
Expression typeRefExpression = Debugger.GetExpression("typeof(" + myExpression.Type + ").FullName"
once you have the TypeFullName, you can search inside your assemblies cache for a matching System.Type, and once you have that, you can dig into it all you want using the standart Reflection API.
Note that this will only work in C#, because of its "typeof" keyword. You'll have to use a different keyword for VB.Net, for example.

Is it possible to search intellisense in vstudio?

Is it possible to search or filter intellisense in visual studio?
Basically i know there is an enum in the project that contains 'column', but the enum doesnt begin with 'c'.
There has been lots of times where id rather not scroll through the hundreds (if not thousands) of valid objects it gives me.
I wonder if the real answer here is (and I won't be surprised to be voted down for this) that your enum isn't properly named. If it was then I'd expect the name to be obvious in the use context, may be consider renaming the enum?
You can search in Class View. Type "column" and hit enter.
Visual Studio 2010 changes all of this, giving you multiple very easy ways to do this type of search quickly.
If you're using ReSharper, you can use "Go To Symbol..." and type "column", and it will give you all symbols (types, properties, fields, methods, etc) that match.
Otherwise your best bet is to use the Object Browser and search.
I really don't know about doing that in intellisense itself, but assuming the objective is to actually find a member whose name you don't remember, you can write a small utility for that purpose using the underlying mechanism intellisense uses, reflection.
Open the Object Browser under View menu. From there, you can search within all the language constructs available to you.

VS tool to find candidates for expressions

Are there any tools that augment/replace intellisense with a search of for expressions that will result in a given type?
I'm thinking of the case where I need to get some particular object and I known the type and will recognize the name but don't know what sequence of objects to jump thought to get to it. Such a tool would do a breadth first search of all options and list anything that has the correct type.
This is an old question, I know, but I'm giving it a shot in case you didn't find anything. Intellisense has the Resolve option (from at least Visual Studio 2005), with which you type your particular object straight, and then with right-clicking you can choose Resolve, which will add the library required to the list at the top of your class.
Similarly, ReSharper might have something fancier for you.

Resources