在Visual Studio中检查使用指令引用 [英] Check using directive reference in Visual Studio

查看:65
本文介绍了在Visual Studio中检查使用指令引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢

当我按照下面的方式使用System.Reactive.Linq 行注释掉该时, Observable 表示该错误在当前上下文中不存在.

>

可以注释掉using指令并像上面那样检查错误,有没有比逐个检查错误更好的方法了?


答案:确实找到用法,光标位置很重要

如下面在答案注释中所述,光标位置需要位于 using 而不是名称空间.



旧问题正文
是否存在一个Visual Studio(或ReSharper)命令,该命令告诉我哪些方法来自 SomeNamespace.SomeClass (由在使用文件中使用SomeNamespace.SomeClass 定义)?

当我遇到一些意外的命名空间时,我想检查用例.
使用指令上的查找用法"或所有引用"仅显示其他指令.

我可以删除指令using并检查文件中的错误,但是我必须不知道更好的方法...


EDIT + EDIT2 :
举个例子可能有助于更好地弄清情况.

SomeClass.cs

  namespace SomeNamespace {公共静态类SomeClass {公共静态字符串SomeExtensionMethod(此字符串s){//一些实现}/*更多扩展方法*/}} 

Consumer.cs

 使用SomeNamespace.SomeClass命名空间OtherNamespace {公共类消费者{公共无效Consume(){字符串ID ="someid";= id.SomeExtensionMethod之后的字符串;}/*使用了SomeClass的更多扩展方法*/}} 

问题是,当我使用SomeNamespace.SomeClass 找到一些具有 Consumer 时,检查底层方法在哪里使用的最佳方法是什么?
是否有类似于查找用法"的命令来获取来自 Consumer 类下的 SomeNamespace.SomeClass 的所有方法?

解决方案

通常只需检查您的用法即可.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.

这篇关于在Visual Studio中检查使用指令引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆