3

Given I am using framework via a NuGet package on my C# project inside Visual Studio and the source code for the framework/NuGet package is available e.g. on Github. How could I add the source code to Visual Studio, so I can browse it?

E.g. I write my own class deriving from a framework base class:

public MyClass : FrameworkClass

It compiles fine as all needed for that is given by the NuGet package. But in addition I now also want to be able to view the FrameworkClass implementation from inside Visual Studio.

Is that somehow possible, given the source code is available e.g. on Github? (it's ok for me to manually download the source code from Github and add it to Visual Studio in addition to the nuGet package, I just don't know if and how that can be done)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • 1
    If the source code is written in a .Net language, you can import the Project(s). You don't need the NuGet package(s) anymore. – Jimi Oct 29 '19 at 10:56
  • 1.As Jimi said, since we can download the project from github to local machine. You can choose to import the project(which contains the source code) into current solution(right-click solution=>import existing ...), and then `right-click your current project=>add reference=>add project reference` to reference that downloaded project. This has no difference than using nuget package, and it will bring the ability to view source code directly. 2. In VS code editor, you can type F12 in one method(function) to navigate to the line where the function is defined. – LoLance Oct 29 '19 at 11:49
  • @LanceLi-MSFT I think your option for decompiling comes from Resharper. Also decompiling likely doesn't give the exact same source code / misses comments etc. at all: I want to continue to use the NuGet packages, so my project can also easily be build in the cloud (currently developing a Xamarin application which is also compiled on App Center which downloads all dependencies using NuGet) – stefan.at.kotlin Oct 29 '19 at 11:58
  • VS now includes [this function](https://docs.microsoft.com/en-us/visualstudio/ide/go-to-and-peek-definition?view=vs-2019#view-decompiled-source-definitions-instead-of-metadata-c) itself even in VS2019 community version. And yes, the decompiling is not always accurate, sorry for the bad idea :( And if you want to view source code during debugging your project, you can refer to [this](https://stackoverflow.com/questions/21857780/how-to-debug-into-my-nuget-package-deployed-from-teamcity/58565483#58565483), if you want to view source code when developing current project... Maybe not supported... – LoLance Oct 29 '19 at 12:31
  • @LanceLi-MSFT Please post as answer. Besides that I am lucky that the nuget package I am mainly interested in seems to support sourcelink, I guess there isn't any other way anyways. And the debugging thing is cool, didn't know about that :-) – stefan.at.kotlin Oct 29 '19 at 12:55
  • typo, meant decompiling, not debugging. – stefan.at.kotlin Oct 29 '19 at 14:07

1 Answers1

2

As I know VS doesn't have the option to support viewing source code of one nuget package directly in one project that consumes that package. At least during developing time(when code in text editor), this is not supported.

But if someone wants to navigate to and view source code of one nuget package during debugging time:

1.One way I once used is to put .pdb file into nuget package, and download the source files in one folder, add path of folder to Debug Source Files setting.

2.Another way is to use Source Link+ nuget way, it's supported in VS2017 and VS2019.

For more details please refer to this thread, and there's some other good workarounds there. Cheers!

cdiggins
  • 17,602
  • 7
  • 105
  • 102
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • hmm, I made a mistake - actually source link is great for debugging, but yeah, I want to be able to explore source code without debugging. My fault and I guess I rashed my own questions asking you to post an answer. Let's see if anyone else replies, otherwise I will accept your answer and ask a new one especially for really only browsing. – stefan.at.kotlin Oct 29 '19 at 14:08
  • Any update feel free to share here :) And if you find the workaround youself, consider adding it as answer to share the useful info! – LoLance Oct 29 '19 at 16:17