且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Visual Studio中检查使用指令引用

更新时间:2023-02-27 10:27:34

通常只需检查您的用法即可.Visual Studio仅允许从一个命名空间调用方法/类.如果您正在使用的多个名称空间中有两个以上具有相同名称的方法,Visual Studio将强制您在调用中指定名称空间显式.

 使用Name1;使用Name2;命名空间Class1 {类测试{公开测试{//此方法仅存在于Name1中方法1();//此方法同时存在于Name1和Name2中Name1.Method2();Name2.Method2();}}} 

因此,如果您仅看到

 使用SomeNamespace; 

您将知道,可能来自该名称空间的任何类都是特定于SomeNamespace的.如果存在另一个具有相同名称但又位于不同名称空间中的类,则Visual Studio将在调用时强制您键入该名称空间.

因此,如果您发现使用情况正常且没有随机性

  RandomNamespace.Class1 class1 = new RandomNamespace.Class1(); 

您肯定知道没有什么不寻常的.

EDIT3: Question rewritten for future reference, thanks to BahJiy

Is there a Visual Studio (or ReSharper) command like "Find Usages" to find class usages from using directive?


Find Usages only shows the reference of the namespace itself (and thus "the only usage" as per below screenshot).

When I comment out that using System.Reactive.Linq line as per below, Observable indicates an error that it doesn't exist in the current context.

I could comment out the using directive and check errors like above, is there a better way than checking errors one by one?


Answer: Find Usages does it, cursor position matters

As mentioned below in the comment to the answer, the cursor position needs to be at using, rather than the namespace.



Old question body
Is there a Visual Studio (or ReSharper) command that tells me which methods are coming from SomeNamespace.SomeClass defined by using SomeNamespace.SomeClass within using file?

I would like to check the use case when I encounter some unexpected namespace.
Find Usages or All References on the using directive only shows other directives.

I could delete the very using directive and check for errors within the file, but I must be ignorant of a better way...


EDIT + EDIT2:
An example may help to clarify the situation better.

SomeClass.cs

namespace SomeNamespace {
    public static class SomeClass {
        public static string SomeExtensionMethod(this string s){
            // some implementation
        }
        /*
            More extension methods
        */
    }
}

Consumer.cs

using SomeNamespace.SomeClass

namespace OtherNamespace {
    public class Consumer {
        public void Consume(){
            string id = "someid";
            string after = id.SomeExtensionMethod;
        }
        /*
            More extension methods from SomeClass used
        */
    }
}

The question is, when I find some Consumer having a using SomeNamespace.SomeClass, what's the best way to check where the underlying methods are used?
Is there a command similar to Find Usages to get all the methods coming from SomeNamespace.SomeClass under Consumer class?

Normally just check your using. Visual Studio only allow a method/class call from one Namespace. If there are two more more method of the same name in multiple of your Namespace that you are using, Visual Studio will force you to specify the Namespace explicative in your call.

i.e.

using Name1;
using Name2;

namespace Class1 {
    class Testing {
        public Testing {
            // this method only exists in Name1
            Method1 ( );
            // this method exist in both Name1 and Name2
            Name1.Method2 ( );
            Name2.Method2 ( );
        }
    }
}

So if you see only

using SomeNamespace;

you will know that any classes that may come from that namespace is specifically from SomeNamespace. If there was another class of that same name but also in a different namespace, Visual Studio will force you to type in that namespace when calling.

So if you see that you usings are normal and there are no random

RandomNamespace.Class1 class1 = new RandomNamespace.Class1 ( );

You know for sure that nothing is out of the ordinary.