我可以重现你的情况:
这种情况源于您正在使用的功能:
Top level statement个
这是从C#9开始的一项新功能.
你的解决方案让VS的智能感知消失的原因有两个:
1, First, the .cs file was not match the regularity of C# project.(In your situation, two .cs file used top level statement, which is incorrect usage.)个
2, Second, in this situation, by setting the file as 100, you're essentially telling Visual Studio that this file is just content (like a text or data file) and not something it should provide C# Intellisense for.
我建议你使用公共程序的入口点而不是顶层语句,这样你就可以在编写代码时使用智能感知并且没有错误输出.
例如:
100
namespace ConsoleApp11
{
internal class Program
{
static void Main(string[] args)
{
//Put the code here.
}
}
}
100
namespace ConsoleApp11.Samples
{
internal class Program
{
static void Main(string[] args)
{
//Put the code here.
}
}
}
请注意,在这种情况下,用法仍然不正确,因为有多个入口点,我们只是在写作时没有遇到问题.
如果你想运行应用程序,你仍然需要像这样排除.csproj文件中的文件:
<ItemGroup>
<Compile Remove="Samples\Program1.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Samples\Program1.cs" />
</ItemGroup>
或使用C# preprocessor directives,例如:
#if NEVER_COMPILE
namespace ConsoleApp11.Samples
{
internal class Program
{
static void Main(string[] args)
{
//Put the code here.
Console.WriteLine("This is program1.");
}
}
}
#endif
在Visual Studio中,智能感知和编译器紧密集成在一起.Roslyn compiler是.NET编译器平台,负责编译代码和提供代码分析服务,这些服务支持智能感知等功能.VS的许多IntelliSense问题也将在此处报告:
https://github.com/dotnet/roslyn/issues个
由于智能感知进程依赖于标记为要编译的文件列表,因此您必须做出 Select .如果通过某种方式将文件标记为编译,则会遇到错误输出.如果从编译中排除该文件,您将失go 大部分智能感知功能.
顺便说一下,您也可以遵循JonasH的建议,在这种情况下使用单元测试项目.那样会更合适.