Henry Yu
Henry Yu

Reputation: 56

Find reference for namespace in Visual Studio

I am working on a c# project. It reference quite a few packages and there are packages referencing other packages. So a namespace used in the program does not necessarily come from a direct reference. For a specific using statement, is there a way to find out which reference (by reference, I mean the external DLL's/NuGet packages) it is originated from? Thank you.

For example project reference a Nuget Package called Package1. In Package1 we have namespace called Namespace1. Then Package1 references Package2, which have a namespace called Namespace2. In your code you could have

using Namespace2;

But how do you know which assembly or Nuget Package Namespace2 is originated from (in this case Package1)?

Upvotes: 3

Views: 3160

Answers (3)

JohnRambo93
JohnRambo93

Reputation: 91

Old post but the situation with built in functionality did not change over the years. I had the same problem and I can't believe you have to depend on external tools for souch a feature. Visual Studio supports the visualization of transitive dependencies in the SDK project formats, but you can't search for it - you have to manually go through all packages (at least in my case).

After some searching i found another stackoverflow post which had the same problem, but there was a solution: The tool Depends is a console tool which lists all direct and transitive packages of a project. You can find the root of the dependency if you navigate using the "Reverse depends" box and press "Enter". So in combination with the Visual Studio feature "Go to definition" (F12), you can now find out where a class or namespace is included and then search for the root nuget package.

Upvotes: 0

Jay Tuan Doan
Jay Tuan Doan

Reputation: 31

I tried harlam357's answer in visual studio 2019 but it failed (the image bellow).

1.

Then, I did a trick. I turned the reference in to a comment and found the error line which implements a method belong the namespace. Through the method I got the root reference.

2.

Upvotes: 3

harlam357
harlam357

Reputation: 1491

I'm not aware of a native means in Visual Studio to do this. However, JetBrains ReSharper can do this. Note that a single namespace import isn't tied to a single assembly. A namespace import (using statement) can easily bring types from several different assemblies into scope.

With ReSharper in hand I simply selected the namespace and pressed F12. You can see that System.Collections.Generic is defined in four assemblies referenced by this project.

ReSharper namespace find

Upvotes: 1

Related Questions