简体   繁体   English

名称 Secrets 是 C# 中类的无效名称吗

[英]Is the name Secrets an invalid name for a class in C#

I am probably making a dumb error but I cannot see it.我可能犯了一个愚蠢的错误,但我看不到它。

I added a custom class to my project followingthe simple Newtownsoft example here .我按照这里的简单 Newtownsoft 示例向我的项目添加了一个自定义类。

namespace MyApp
{
     public class Secrets
     {
          public string Test {get;set;}
     } 
}

But when I go to reference the class in my Program.cs:但是当我在 Program.cs 中引用该类时:

      namespace MyApp
      {
          static class Program
           {
              [STAThread]
              static void Main()
               {
                 <snip>

                 string json = FetchJson();
                 var MySecrets = JsonConvert.DeserializeObject<Secrets>(json);   // this line
               }
           }
      }
 

Visual Studio underlines <Secrets> and says the type or namespace 'Secrets' cannot be found. Visual Studio 在<Secrets>下划线并表示找不到类型或命名空间“Secrets”。 But the project compiles without error.但是项目编译没有错误。

If I change the name of the class to Sekrets then Visual Studio finds it and is happy.如果我将类的名称更改为Sekrets那么 Visual Studio 会找到它并且很高兴。

PS And if I then rename the file from Sekrets.cs back to Secrets.cs and let VS make the global change, then VS remains happy, no error, no red underlining. PS 如果我然后将文件从Sekrets.cs重命名回Secrets.cs并让 VS 进行全局更改,那么 VS 仍然很高兴,没有错误,没有红色下划线。

PS And if I then rename the file from Sekrets.cs back to Secrets.cs and let VS make the global change, then VS remains happy, no error, no red underlining. PS 如果我然后将文件从 Sekrets.cs 重命名回 Secrets.cs 并让 VS 进行全局更改,那么 VS 仍然很高兴,没有错误,没有红色下划线。

Sounds like an Intellisense database problem, you generally fix those by closing VS and then deleting your .vs folder.听起来像是 Intellisense 数据库问题,您通常通过关闭 VS 然后删除.vs文件夹来解决这些问题。 Next time you open your solution, VS will rebuild the database from scratch.下次打开解决方案时,VS 将从头开始重建数据库。

Luckily it almost never happens in C#, since it's a much easier language to parse than C++.幸运的是,它在 C# 中几乎从未发生过,因为它是一种比 C++ 更容易解析的语言。 Intellisense really has problems parsing that. Intellisense 在解析它时确实存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM