Visual Studio说名称在当前上下文中不存在 [英] Visual Studio saying name doesn't exist in current context

查看:257
本文介绍了Visual Studio说名称在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样的类上调用静态方法

I am calling a static method on a class like

Foo.bar()

Visual Studio的智能感知功能可以识别Foo并为我自动完成设置(它突出显示了Foo,并且一切正常.)一切正常,直到我去构建项目为止,并且抛出一个错误,说当前环境中不存在名称Foo.

Visual studio's intellisense recognizes Foo and autocompletes bar for me (it highlights Foo and everything like it is working fine). Everything looks fine until I go to build the project, and it throws an error saying the name Foo doesn't exist in current context.

我正在其他文件中使用此静态方法调用,所以我知道该类是可以的.这种情况太大了,无法发布代码,所以我主要是在寻找开始研究的原因,这将导致智能感知正常运行,但在这样的编译过程中会出错.

I am using this static method call in other files, so I know the class is ok. The situation is too big to post code, so I am mostly looking for reasons to start looking into that would cause intellisense to function normally but get errors on compile like this.

推荐答案

当名称空间,类和变量具有相同的名称时,如果它们纠缠在一起,就会发生这种情况.我以前曾遭受过这种痛苦.Intellisense告诉我我是对的,编译器告诉我我是错的!我相信编译器!

This can occur when namespaces, classes and variables become tangled when they have the same name. I have suffered with this before. Intellisense told me I was right, the compiler told me I was wrong! I trusted the compiler!

您有2个我能想到的选择

You have 2 options that I can think of

  1. 在代码中搜索Foo,并查看其是否用于静态类之外.

  1. Search your code for Foo, and see it it is being used for something other than the static class.

完全限定Foo.bar()调用. MyApplication.This.That.Foo.bar();

Fully qualify the Foo.bar() call. MyApplication.This.That.Foo.bar();

按此顺序执行...最好以优雅的方式解决问题,因此您只需调用Foo.bar()即可,因为与拥有 MyApplication.This.That.Foo.bar(); 到处都是!

Do it in that order...it's better to elegantly resolve the issue so you can just call Foo.bar() as this is more readable and maintainable than having MyApplication.This.That.Foo.bar(); all over the place!

这篇关于Visual Studio说名称在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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