My C# project references a third-party DLL for which I have the source code. Can I somehow tell Visual Studio the location of that source code, so that, for example, when I press F12 to open the definition of a method in the DLL, it will open up the source code, instead of opening up the "Class [from metadata]" stub code?
3 Answers
If you use ReSharper, you can enable it by going to ReSharper / Options / External Sources, and move up "Sources from symbol files". Then in the tab "Sources from symbol files", click "Advanced" and there you can map source folders.
-
16What is the 'target folder' for that dialog?I just have a DLL and a folder of sources I would like to attach for navigation purposes.– IainCommented Nov 7, 2013 at 22:48
Looks like the answer is near the bottom of this MSDN documentation page.
The debugger looks for source files in the following locations:
Files that are open in the IDE of the Visual Studio instance that launched the debugger.
Files in the solution that is open in the VS instance.
Directories that are specified in the "Common Properties" / "Debug Source Files" page in the properties of the solution.
The source information of the .pdb of the module. This can be the location of the source file when the module was built, or it can be a command to a source server.
To add a directory to the solution's Debug Source Files page (step 3. above):
You can specify a network or local directories to search for source files.
Select the solution in Solution Explorer and then choose "Properties" from the shortcut menu (i.e. right-click context menu).
Under the "Common Properties" node, choose "Debug Source Files".
Click the folder icon. Editable text appears in the "Directories containing source code" list.
Add the path that you want to search.
Note that only the specified directory is searched. You must add entries for any subdirectories that you want to search.
So if you just want to debug a specific file once without cluttering up your solution, just open that file up in the Visual Studio IDE; if you need to step from that file into others, you will likely need to have the other files open in Visual Studio as well.
If you often find yourself needing to debug source files outside of your solution, then you can either:
- Add the source files to one of your existing projects in the solution, or create a new project in the solution to house the source files. This will clutter up your solution/project, but since it will be checked into source control, all team members will automatically be able to debug into the source files.
- Add the directories holding the source code to the solution's "Debug Source Files". Because this change is not checked into source control, each team member that wants to debug the source files will need to do this and add their local paths. Also, if you get the solution code on a different machine, you will need to remember to perform this step again.
-
5Unfortunately this only works if you debug a running program that uses the assembly. What Joe was looking for is a way to do code navigation (e.g. find definition, usages, etc.) in the IDE without running the code. If ReSharper has solved this problem, great. I am hoping that more recent versions of Visual Studio also have the ability built in. But I cannot find it. Any one knows?– MinyuCommented Jan 17, 2015 at 19:48
-
2Resharper is the only way I could find that does this. I am amazed at the fact that Visual Studio 2015 Enterprise (such an expensive piece of software) doesn't support such a simple feature which happens to be just an afterthought in "enterprise" grade IDEs such as Eclipse, a free and opensource piece of software. Commented Jul 20, 2017 at 12:30
-
@netlander can you provide instructions to do it with ReSharper?. Commented Mar 1, 2019 at 16:59
One way you could do this would be to create another project in your solution, put the DLL source in there, and then from your main project, add the dependency as a project reference instead of an assembly reference. That should let you browse the source / step into it while debugging, etc.
There might be an easier way to do it, but I'm not aware of any at present.
-
11It clutters the solution a bit, and it means I am compiling the third-party DLLs myself, which feels a little strange/slower. I was hoping Visual Studio had the equivalent to Eclipse's "attach source" option when referencing a JAR file. Commented Apr 12, 2010 at 7:04
-
1Yeah, I know it's less than ideal. If someone else has a better solution, I'd be interested to know myself.– tzamanCommented Apr 12, 2010 at 8:09
-
1Resharper is the only way I could get this to work, see my comment above. Commented Jul 20, 2017 at 12:31