Search project for where class is instantiated - visual-studio-2010

Is there a tool in visual studio 2010 to scan a project and find out where a specific class is instantiated?

Shift + F12 will find all usages.
Simply select or move the cursor onto a type and press “Shift + F12”:

I realise this was asked a long time ago but I'm adding this in case someone stumbles across this in a search engine looking something similar.
If you have ReSharper you can use Find Usages (Shift-F12 by default) and then you can filter by type of usage in the results. New instance creation is one such filter which is likely to find all cases of instantiation (reflection aside).

Related

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?
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.

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.

Performing expression evaluation/reduction in Visual Studio 2008

Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?
For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?
Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?
Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.
Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)
Thank you for the above answers.
There probably are better ways, but here's a quick and dirty macro that does what I need.
References to the System.Data and System.XML namespaces need to be added.
Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.
Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".
Sub Eval()
Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
Using dt As New DataTable()
dt.Columns.Add("Expression", GetType(Double), ts.Text)
dt.Rows.Add(dt.NewRow)
ts.Text = CDbl(dt.Rows(0).Item("Expression"))
End Using
End Sub
Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.
Also, it would be helpful to know the language you are working in?
Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.
Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)
Set a breakpoint in your program that's likely to get hit when you debug the program.
Then, run your program under the Visual Studio debugger.
When the breakpoint is hit, open the Watch window.
In the Watch window, add a new watch by clicking in the Name column.
Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter].
... You should see the Value column get populated with -0.35.
Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.
You could just go to the immediate window and type "?<yourExpression>"

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.

Visual Studio refactoring: Remove method

Is there any Visual Studio Add-In that can do the remove method refactoring?
Suppose you have the following method:
Result DoSomething(parameters)
{
return ComputeResult(parameters);
}
Or the variant where Result is void.
The purpose of the refactoring is to replace all the calls to DoSomething with calls to ComputeResult or the expression that uses the parameters if ComputeResult is not a method call.
If I understand the question, then Resharper calls this 'inline method' - Ctrl - R + I
I would do it the simpliest way:
rename ComputeResult method to ComputeResultX
rename DoSomething method to ComputeResult
remove DoSomething method (which is now ComputeResult)
rename ComputeResultX method back to ComputeResult
Maybe VS will show some conflict because of the last rename, but ignore it.
By "rename" I mean: overwrite the name of the method and after it use the dropdown (Shift+Alt+F10) and select "rename". It will replace all occurences with the new name.
There are a few products available to add extra refactoring options to Visual Studio 2005 & 2008, a few of the better ones are Refactor! Pro and Resharper.
As far as remove method, there is a description in the canonical Refactoring book about how to do this incrementally.
Personally, I follow a pattern something along these lines (assume that compiling and running unit tests occurs between each step):
Create the new method
Remove the body of the old method, change it to call the new method
Search for all references to the old method (right click the method name and select "Find all Reference"), change them to calls to the new method
Mark the old method as [Obsolete] (calls to it will now show up as warnings during the build)
Delete the old method
When it comes to refactoring like that, try out ReSharper.
Just right click on the method name, click "Find usages", and refactor until it cannot find any references.
And as dlamblin mentioned, the newest version of ReSharper has the possibility to inline a method. That should do just what you need.
ReSharper is definitely the VS 2008 plug in to have for refactoring. However it does not do this form of refactoring in one step; you will have to Refactor->rename DoSomething to ComputeResult and ignore the conflict with the real ComputeResult. Then delete the definition which was DoSomething. It's almost one step.
However maybe it can do it one step. If I read that correctly.
You can also right click the method name and click "Find all References" in Visual Studio.
I personally would just do a CTRL + SHIFT + H to Find & Replace

Resources