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

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

Related

Find all references and Go to definition not working for a specific project in vs solution

In a VS 2013 solution with several projects I have added an existing project (dll) and referenced it in one of the other DLLs.
The problem is that when I use "Go to definition" for a class that is defined in this project the text from the metadata is displayed as if the text files of this dll are not in the solution.
Similar problem with "Find all references".
Language is the same C#.
Any ideas?
Problem solved.
When I added the reference of the existing project that I added in the solution I did not select it from the projects of the solution but browsed and select it from its output folder (debug or release).
That simple!

Visual studio browse references source?

I remember being able to do this but I must have changed something as I can no longer view an external projects source even though the pdb is with the reference. I can step into external functions when debugging but I can't go to them using "Go to implementation/declaration"
This will work if you have a separate class library Project as a part of your Solution, and you added a reference to the Project, not the compiled .dll.
You cannot step into a .dll when debugging.
It's all in how you add the 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.

How can I reference a dll in the GAC from Visual Studio?

This assembly is in the GAC: Microsoft.SqlServer.Management.RegisteredServers.dll
How can I add a reference to this assembly in Visual Studio?
I can view the file in c:\windows\assembly\
As the others said, most of the time you won't want to do that because it doesn't copy the assembly to your project and it won't deploy with your project. However, if you're like me, and trying to add a reference that all target machines have in their GAC but it's not a .NET Framework assembly:
Open the windows Run dialog (Windows Key + r)
Type C:\Windows\assembly\gac_msil. This is some sort of weird hack that lets you browse your GAC. You can only get to it through the run dialog. Hopefully my spreading this info doesn't eventually cause Microsoft to patch it and block it. (Too paranoid? :P)
Find your assembly and copy its path from the address bar.
Open the Add Reference dialog in Visual Studio and choose the Browse tab.
Paste in the path to your GAC assembly.
I don't know if there's an easier way, but I haven't found it. I also frequently use step 1-3 to place .pdb files with their GAC assemblies to make sure they're not lost when I later need to use Remote Debugger.
Registering assmblies into the GAC does not then place a reference to the assembly in the add references dialog. You still need to reference the assembly by path for your project, the main difference being you do not need to use the copy local option, your app will find it at runtime.
In this particular case, you just need to reference your assembly by path (browse) or if you really want to have it in the add reference dialog there is a registry setting where you can add additional paths.
Note, if you ship your app to someone who does not have this assembly installed you will need to ship it, and in this case you really need to use the SharedManagementObjects.msi redistributable.
I've created a tool which is completely free, that will help you to achieve your goal. Muse VSReferences will allow you to add a Global Assembly Cache reference to the project from Add GAC Reference menu item.
Hope this helps Muse VSExtensions
In VS2010, from the Add Rerences window you can click 'Browse' and navigate to C:\Windows\Assembly and add references to the assemblies that you want. Please note that the files may be grouped under different folders like GAC, GAC_32, GAC_64, GAC_MSIL etc.
In VS, right click your project, select "Add Reference...", and you will see all the namespaces that exist in your GAC. Choose Microsoft.SqlServer.Management.RegisteredServers and click OK, and you should be good to go
EDIT:
That is the way you want to do this most of the time. However, after a bit of poking around I found this issue on MS Connect. MS says it is a known deployment issue, and they don't have a work around. The guy says if he copies the dll from the GAC folder and drops it in his bin, it works.
The only way that worked for me, is by copying the dll into your desktop or something, add reference to it, then delete the dll from your desktop.
Visual Studio will refresh itself, and will finally reference the dll from the GAC on itself.
Assuming you alredy tried to "Add Reference..." as explained above and did not succeed, you can have a look here. They say you have to meet some prerequisites:
- .NET 3.5 SP1
- Windows Installer 4.5
EDIT: According to this post it is a known issue.
And this could be the solution you're looking for :)
May be it's too late to answer, but i found a very simple way to do this(without a hack).
Put your dll in GAC (for 3.5 Drag Drop inside "C:\Windows\assembly\")
GoTo Projects --> Properties
Click Reference Path (for 3.5 it's "C:\Windows\assembly\")
and Build
Hope it helps
The relevant files and references can be found here:
http://msdn.microsoft.com/en-us/library/cc283981.aspx
Note the links off it about implementation/etc.
I found this extension for VS 2013 Vitevic GAC Reference.

Resources