Is there a simple way to include a missing reference to an assembly in Visual Studio? - visual-studio

I'm using Visual Studio 2012.
In most solutions I have lots of projects which reference each other.
It's quite time consuming to always add the reference manually.
Is there a simple way (either built-in or via extension) to include a missing reference to an assembly in the own solution similar to the way missing usings are resolved?

Resharper does exactly this. If there's a class that is defined in another project in the solution, it offers you to reference that class and import the namespace. All you do is press "Alt+Enter, Enter" to pop up context menu and select the first option.

As mentioned before, resharper will be a good solution for your request. but if you are looking for a free visual studio extension that does the same you can use 'WoVS Quick Add Reference' extension.
Some info: http://ihadthisideaonce.com/2011/07/29/recommended-wovs-quick-add-reference/
Download link: https://visualstudiogallery.msdn.microsoft.com/dc06b54c-b6c4-4cf5-8203-a09c6979e881/
I hope it supports your questions..

This extension at least offers a way to add references to GAC assemblies
visual studio gallery
But it requires CodeRush and so also isn't a free choice.

Related

Visual Studio dependencies between projects: automatically open up related solution when browsing through code references? [duplicate]

I have a C# project which contains references to assemblies in DLL format. I have the PDB information for these DLL files, contained in the same folder. When I press F12 on a referenced member, I want to go to the definition of the member. Instead, it gives me the metadata, which of course I don't want.
When VS is debugging, it does go to the source code if I single step into that method. But if I press F12 on that method, it still brings me to the metadata.
There is a similar question here, but it only applies to project references, and the accepted solution has nothing to do with my issue.
"Go To Definition" in Visual Studio only brings up the Metadata
To summarize:
Referenced project is a DLL
I have the PDB information
Single-stepping in debug-mode brings me to the source code
F12 brings me to metadata
I want F12 to bring me to the source code as debug does.
EDIT: Adding as a "Project" is not an option as it creates additional complexity as our solution file references about five other projects which are all under separate source control repositories.
In Visual Studio expand the References section, select the reference to your related project (the one that contains the source code you want to F12 into) right click on it and select "Remove".
Then add the reference back to the project by right clicking on References and selecting Add Reference, under the Projects tab (if your using VS 2012) select (or browse for) the project you want to add a reference to and then click the OK button.
This will rebuild the reference and you will be able to F12 into the referenced projects source code. You will need to do this with all the projects in your solutions that are having this issue.
I don't know why this happens but at least the solution to the problem is rather simple
If you have ReSharper installed, you should bring up ReSharper options, and look for External Sources. There you can specify the relevant options:
It appears that this issue has been solved in Visual Studio 2013. Having the PDB information in the same folder as the DLL is showing me the source code when I use Go To Definition. I do not have the Reflector extension installed.
Is the referenced DLL a project in your solution? I find that when I have the source code for the project, and it's in my solution, Visual Studio is able to link to this code much more easily. (without showing me meta data).
Also, be sure to add the reference by "Project" in the References popup.
This is generally what I do anyways when I have this problem
Hope this helps!
PS. PDB files are usually just for debugging (both locally and remote) and are not used for source code reading in the way you are attempting to use it. ("Go To Definition")
One solution to this issue is to use .NET Reflector, VS or VSPro edition. This program will modify Visual Studio to provide the required functionality.
http://www.reflector.net/
Unfortunately, it costs $135 to $195, which isn't an option for everybody.
Add the reference as a project instead of ..\bin\Debug\referenceFile.dll
That solved my issue
I also used add reference as a project and my problem has been resolved and it's working great. Actually I was stuck at this point from very long time and finally i resolved this issue.
References -> Add References -> Solution -> Projects -> Select reference

Visual Studio 2010 - Add Reference Tabs will only give me projects. No Browse Allowed?

My problem seems pretty simple. I have a Solution with multiple projects and basically I am trying to do some mixing with C++, C# and CLI. My problem is that I need to add references from my c++ project. When the "Add Reference" dialog comes up, I only get tab for "Projects".
I know that usually you get multiple tabs for adding different reference types. What I am interested in is the "Browse" dialog. I need this to add a reference to a dll that is prebuilt. I am targeting .Net 4.0.
It seems like there is a project property that can be set to limit this but I'm not sure. MSDN seems to mention something along the lines that if you have multiple projects in a solution and they target different versions of the .Net framework, the list of possibilities may be limited.
In "How to: Add or Remove References in Visual Studio" MSDN mentions: "The number of tabs available at the top of the Add Reference dialog box can vary, depending on the type of project open and the resources it is using. C++ native projects contain only a Projects tab."
It doesn't really mention what these are specifically so maybe its just a broad subject. Anyways, I have stripped down the solution to only my C++ project and I can build successfully. However, I can only add References to other projects.
Apparently, setting the CLR option on the project is what opens up the list to include more options. I only wanted to enable it on one source file, but I guess Visual Studio will not add reference capabilities unless you turn it on for the whole project.
Therefore, my solution was to enable it for the whole project, add the reference, and then remove the option on the project. Basically a dirty trick but just in case people run into the same issue, that is what worked for me.
Erik

Removing all unused references from a project in Visual Studio projects

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?
In your answer, please specify which version of VS the solution applies to.
If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...
http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html
This feature does not correctly handle:
Dependency injected assemblies
Dynamically loaded assemblies (Assembly.LoadFile)
Native code assemblies loaded through interop
ActiveX controls (COM interop)
Other creative ways of loading assemblies
All you need is stone and bare knuckle then you can do it like a caveman.
Remove unused namespaces (for each class)
Run Debug build
Copy your executable and remaining namespace references to new location
Run the executable
Missing Reference DLL error will occur
Copy required DLL from Debug folder
Repeat 4-6
Gu Gu Ga Ga?
Throw your stone
You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.
The Resharper extension will do this for you.
This extension supports Visual Studio 2005 through 2017.
While the compiler won't include unused assemblies, extraneous using statements and references slows down Visual Studio and Intellisense, since there's more code the tools have to consider.
You can try the free VS2010 extension: Reference Assistant by Lardite group. It works perfectly for me. This tool helps to find unused references and allows you to choose which references should be removed.
In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn´t such a function.
The only way to do it in a C# project (without other tools) is to remove possible unused assemblies, compile the project and verify if any errors occur during compilation. If none errors occur you have removed a unused assembly. (See my post)
If you want to know which project (assembly) depends on other assemblies you can use NDepend.
With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.
Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.
For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:
reference is removed in the project
project is compiled with msbuild
check for build errors
restore removed references if there were build errors.
For anybody coming here looking for Visual studio 2012:
Download and Install Reference Assistant for Visual Studio 11
Later you can do:
In Visual Studio 2013 this extension works:
ResolveUR
Some people suggested to use an awesome tool - Reference Assistant for Visual Studio. The problem is that VS2012 is the latest supported Visual Studio. But there is the way to make it work in VS2013 as well ;)
And here is how:
1) Download Lardite.RefAssistant.11.0.vsix
2) Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip
3) Unzip and open the extension.vsixmanifest file in the text editor
4) Find all occurences of InstallationTarget Version="[11.0,12.0)" and replace them with InstallationTarget Version="[11.0,12.0]" (note the closing bracket)
5) Save the file and zip all files so they are on the root zip level
6) Change the extension of the new zip to vsix
7) Install and enjoy :)
I've tested it with VS2013, thanks source for the tutorial
EDIT
Add to support VS 2015 Community Edition
<InstallationTarget Version="[14.0,15.0]" Id="Microsoft.VisualStudio.Community" />
Meaning of the brackets
[ – minimum version inclusive.
] – maximum version inclusive.
( – minimum version exclusive.
) – maximum version exclusive.
[Update] This feature is only available for .Net core projects.
This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1.
This option is turned off by default, but you can enable it under menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer (Experimental). Once the option is enabled, the Remove Unused References command will appear in the right-click menu of a project name or dependencies node.
You can use Reference Assistant extension from the Visual Studio extension gallery.
Used and works for Visual Studio 2010.
In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).
If it is available you can find it in the project context menu:
You get to choose what to do with each finding.
Read more about it here.
Pro-tip: Check if your project compiles and runs correctly after applying this. In my experience it doesn't check whether a dependency is used at runtime, for instance.
Using DevExpress, I follow these instructions:
In VS, go to DevExpress - Editor - Code Cleanup. Under Rules, check 'Remove unused namespace references'. Click OK.
Right-click on the solution, and choose 'Code Cleanup'. The cleanup runs for a few minutes, and finishes.
Build your application
The following method does not depend on any 'add-on's and is not very painful.
Step through each of your source files and
Select all (Ctrl-A)
Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.
Scanning each file looking for '0 reference' takes only a second.
Scanning an entire project takes only a couple of minutes.
In VB2008, it works this way:
Project>Add References
Then click on the Recent tab where you can see list of references used recently. Locate the one you do not want and delet it. Then you close without adding anything.

Visual Studio and finding References fast

I am building a class library, and I am getting an error and I know that I need to add a reference.
Does VS have a feature where it will find the required library automatically, or do I need to know where the library is located and add the reference manually.
In java/eclipse, you may right click like and jars will be found automatically if on the build path, does VS have this concept?
We use Resharper for that feature and a lot of many other improvements to the IDE.
Couldn't use Visual Studio anymore without it.
ReSharper has a feature to do this. If it knows which assembly has the referenced type, it will suggest adding that assembly as a reference.
If you go alt+shift+F10 on the item that's missing a using it will suggest you the appropriate option (but you obviously need to have the assembly referenced already).
I am not aware of any way of running this on all the items in a single go though (without ReSharper).

Watch for addition of a referenced library to a project (Visual Studio 2008 plug-in)

I am hoping someone can help me with this. What I have been looking around for but can't seem to find is if there is someway to add an event, or anything else that will allow a visual studio plug-in to watch for an external reference to be added to a currently open solution / project?
Or even better yet would anyone happen to know of an add-in that automatically adds all controls from a referenced dll into the visual studio toolbox?
Thanks.
Not exactly what you asked, but I've written a Visual Studio 2008 Integration Package, and if I recall correctly I could override the "add reference" method, as well as inherit the "reference node" and thus change its behavior, for example its constructor.
This functionality might be missing from an add-in (which is more limited than an integration package), so you might consider writing a package instead.
I have written a macro that will change a file reference to a project reference if you add the project to the solution (and vice versa), but I don't think that's what you want?

Resources