How to find derived class in Visual Studio 2017? - c#

I don't have Resharper installed.. I want to see all subclasses inherited from IActionResult and there is no things like show derived classes in object browser and class view.
I try to install a extension TypeHierarchyViewer(https://marketplace.visualstudio.com/items?itemName=munyabe.TypeHierarchyViewer) but it is not working (even using the example List, just remains blank) .
So what should I do?
I use this function frequently developing java application using eclipse..It seems there is no out-of-box tool in VS2017 or I just missed some things??
(Go To Implementation says the symbol has no implementations)
(Same in class view)
I have searched many "solutions"(like Visual Studio: How do I show all classes inherited from a base class?) but not work or need other tools(or just see the doc?).
I want to see if there anyway to do it just using VS.
Before I thought VS was a very good IDE but I can't image it lacks so much basic functions...(so there is Resharper...)
And I find there is derived types but in Solution explorer:
But you can't input the class you want And if I input IActionResult in search box it will not find it(not in the my source and I don't implement it).
After trying..I find VS support it in solution explorer...
But it's hard to use...
I need to find some classes or interface in my source code related to the class or interface I want and use derived type and implements to find the it..
It looks like:
(I find a class and navigate to object, it lists all classes .Then I find ActionResult and choose implements find IActionResult, finally I can see all derived classes above... ...)

emmmmm
it seems there is no direct way using VS(although I can get all sub classes using solution explorer but it's too verbose...) to get the result I want.
Finally I choose to use dotpeek(I don't want to buy resharper because I just study ASP.net core not for work)
I open C:\Program Files\dotnet\sdk\NuGetFallbackFolder in dotpeek where the dependencies located.
Then using Hierarchies get the result.
feel a little disappointed about VS. Not so strong before I heared.
Thx for help. :)

Related

Get all startup projects in solution

As the title suggests I want to query all the Startup Projects set in a IVsSolution/IVsHierarchy, also I would love to achieve that without the DTE needed. Therefor DTE2.Solution.SolutionBuild.StartupProjects isn't really a suitable option for me. I have look into the MSDN, but didn't find quite what I needed. I came across the IVsSolutionManager with its get_StartupProject method. Unfortunately it will only return a single startup project. Is this even doable with using the DTE?
It appears, the EnvDTE.Solution.SolutionBuild.StartupProjects is the only way to do this. I did a walk through of the underlying VS IDE code base, and the startup projects are denoted by an internal flag on an internal C++ class tracking all the loaded projects. Unfortunately, neither that particular class or it's m_dwStartupOpt member are directly (or indirectly exposed.
So your best/only option here is to use that StartupProjects property.
Sincerely,

Generating MSDN documentation URLs

This question is on behalf of one of my team members: I am a developer in charge of writing the documentation for our product. I have written a tool in C# to output our assembly in markdown style files. In order to facilitate the ease of use for our classes, I wanted to implement a way of linking the class type and property constructs to any MSDN documentation available publicly. For the most part, this was accomplished simply by using the namespace of the class like so:
msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol(v=vs.110).aspx
However, I ran into some problems when looking at classes with generic type arguments and properties. They seem to be generated in a special manner that looks like a hashed string, like so:
msdn.microsoft.com/en-us/library/b682ts2x(v=vs.110).aspx
The “b682ts2x” part of the URL is the part that is different.
I would like to know if there is any way I can get in touch with someone who knows how these links are generated, and if there is a way to generate the same exact URL portion (that is, b682ts2x) for any class property using only reflection.
As an alternative approach you could use the same syntax that the F1 help is using when you highlight a class name for example.
As mentioned in Visual Studio intercepting F1 help command
msdn.microsoft.com/query/dev11.query?
appId=Dev11IDEF1&
l=EN-US&
k=k(width);
k(vs.csseditor);
k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);
k(DevLang-CSS)&
rd=true
The "k" parameter above is contains the help context inside visual
studio. Help context contains both "keywords" (text strings) and
"attributes" (name/value pairs) which various windows inside Visual
Studio use to tell the IDE about what the user is doing right now.
For example here is one for System.Net.HttpHttpClient.
https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Net.Http.HttpClient);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true
Notably when I pressed F1 when highlighting "HttpClient" it assumed I meant ServiceClient.HttpClient so be careful to provide the namespace.

Create template for abstract procedures in VS

This is more of a design issue with Visual Studio [>=2010].
Example: When you implement IDisposed in a VB environment, in the body of Public Sub Dispose there are numerous comments and a branch about how to do it. Also the field disposed is automaticaly inserted to the Class.
Now when I design an Interface or abstract Class, is there a way to define some kind of 'template' for each abstract procedure/property body, containing comments or even branches/loops?
Edit:
Now i realize that the C# environment has a different approach to these templates/snippets, but i'd like to make them available for developers in both environments.
These code snippets should be inserted preferebly when using Implement Interface or Implement abstract Class or when TABing after selecting an overridable procedure.
Edit2:
Ok, the Snipped Editor did indeed provide a reasonable solution.
Essentially i have to update my custom snippets for each target instance of VS at the path
C:\%ProgramFiles%\Microsoft Visual Studio %VSVER%\%ENV%\Snippets\%LANG%\...
I hoped there was a way to provide VS with the snippets via the referenced Assembly/Resource only.*

Finding unused classes in C# app

I'm a C#/.net/Visual Studio noob. I inherited a half-completed C# application for a mobile phone. In the course of debugging, I came across several half-finished classes that don't seem to be used anywhere else in the code. Is there a way to get determine if a class definition is instantiated anywhere?
The quickest way (in Visual Studio) is to right-click the type name and select Find all references from the context menu. This will show you all places where that type is referenced in the current solution.
You should get Resharper - it will show "dead" code in grey and make refactoring a lot easier! You may also prefer CodeRush.
Without ReSharper or a similar tool, you can always do a file search for "new ClassName(" in the entire solution.
I usually start with Shift-F12 (or right-click on class name and select "Find All References")
Unless you know the code, and the modules that may use it., CodeRush or Resharper are your better choices.
None of the other answers mentioned the modifiers which can be applied to classes/functions. You certainly want to take scope into consideration before deleting code. You may have other assemblies which use classes/functions.
Remove them from the project and let your unit tests (ahem, you have those right?) and your QA team (you have that right?) identify the problems.
Jokes aside, if it's SO obvious that it's not complete, why not simply remove the code and recompile?
The next steps I would take would be to use a tool like "Find All References" or Resharper (does it even have a feature to do that?)
You can list all the classes (searching for class [a-zA-Z0-9_]+), and then search for new <classname>. The ones not found at the second search are not used. Of course, a simple script in your favourite script language would help.
You'll need however to filter out the classes that are used as base classes of used classes.
Note that this way you'll not find the classes which are used only from unused classes, so several iterations might be needed. Moreover, if some two classes are using each other (but not used from outside), removing them might need additional effort.
Edit:
A better approach would be building dependency tree: for each of the classes you define which class is used by that class, and which class is a base class for that class. This way you find which classes are required for every single class. Then, you can define which classes are required (directly or indirectly) from the class containing Main. All other classes are "unreachable" and therefore not used.
This approach will however remove the classes instantiated by reflection. Well, there is no way to find out at compile time, which classes are going to be instantiated by reflection anyway.
Maybe using the ready tools (like others proposed) is a simpler alternative.

How do you find all implementations of an interface?

Suppose you have an interface defined in C#. What is the easiest method to find all classes that provide an implementation of the interface?
The brute force method would be to use "Find References" in Visual Studio and manually look through the results to separate out the usages from the implementations, but for an interface in a large codebase that is heavily referenced with relatively few implementations, this can be time consuming and error prone.
In Java, running javadoc on the codebase (using the -private option to include private classes) would generate a documentation page for the interface (e.g. Comparable) that includes all implementing classes for the interface as well as any subinterfaces (though it doesn't include implementing classes of the subinterfaces, these are relatively easy to determine by drilling down into the listed subinterfaces). It's this functionality that I'm looking for but with C# and Visual Studio.
In plain Visual Studio (since 2010) you can right click a method name (definition in interface or implementation in other class) and choose View Call Hierarchy. In Call Hierarchy window there is "Implements" folder where you can find all locations of the interface method implementation.
(Edit based on comment...)
If you have ReSharper installed:
In Visual Studio, right click on the type name and choose "Go to Inheritor". Alternatively, select the type name, then go to ReSharper/View/Type Hierarchy to open up a new tab. (The menu will show you the keyboard shortcut - this can vary, which is why I explained how to find it :)
If you don't have ReSharper:
You can use Reflector, which is able to show you all the type hierarchy very easily - just under the type name are expandable items for base types and derived types. Similar tools are available such as ILSpy and dotPeek.
Buy ReSharper - it's a great tool :)
Put the cursor to class or interface type and
CTRL + F12
With Visual Studio 2010+
Right click a member method and choose view call hierarchy. Expand the Implements folder. This lists all the types that implement the interface the method belongs to.
With Resharper 7
Right Click the interface > Navigate To > Derived Symbols. The symbols listed in bold derive directly from the interface. Non-bold symbols derive from a superclass.
For those using Visual Studio 2015, there is this awesome extension called Go To Implementation. Give it a try.
Once you've installed the extension, you can just right click at any occurrences of the interface (e.g. IUrlShortener) and click on Go To Implementation menu. If you only have one class that implements the interface, clicking the menu will bring you directly to the class. If you have more than one class that implements the interface, it will list all the classes.
For those using Visual Studio 2015, install Visual Studio 2015 Update 1 RC. From the Visual Studio blog:
Today we released Visual Studio 2015 Update 1 RC, which builds on the Update 1 CTP we released three weeks ago. In addition to the features introduced in the CTP as described here, the Release Candidate includes the following:
Go To Implementation. The feature many of you have been waiting for: just right-click on an interface or abstract method and select this command to navigate to the implementation.
I don't think this functionality is inbuilt into VS but IIRC Resharper has this.
You could do a regular expression search for the interface.
:(\ *[^},]+,)*\ *IMyInterfaceName
CTRL+SHIFT+F launches the following window:
I prefer the "Navigate To..." option. With your cursor on the function call, try the following:
Shortcut Key:
Ctrl+, (Ctrl+comma)
Menu:
Edit Menu
Click "Navigate To..."
Benefits:
Doesn't show all references like "Find All References"
Shows the
"type" of the implementation so it will note which is your interface
Use Shift + F12 to show all references, including the definitions.
If you use resharper ALT + END shortcut might help to find all Inheritors.
I've heard tell (no experience myself) that doxygen is to .Net as as javadoc is to java.

Categories

Resources