Search code examples
c#visual-studiomsbuildcsproj

(Visual Studio/C#) Add file to Intellisense, but don't compile


I'm writing a C# library at work for internal use. The use cases are pretty specific, so I want people who use it to have a couple samples Program.cs to get them started.

The problem I have is that if I do nothing, I get a bunch of errors "Only one compilation unit can have top level statements", which is expected because of my samples, so I add a:

<ItemGroup>
    <Compile Remove="Samples\Program-*.cs"/>
    <Content Include="Samples\Program-*.cs"/>
</ItemGroup>

The first line flat-out removes the files from solution, where as the second adds them back as content files.

At this point everything works, but I am very annoyed with this solution because I can't use auto-completion or syntax highlighting while writing those sample programs, which makes the process unpleasant. Let me be clear, it's not then end of the world, but would it be possible to keep all of the good stuff Visual Studio provides, without compiling?

(I looked at that question and it's not quite what I need, this question if I understand the accepted answer properly works for projects, I want it to work for specific files.)

Thank you!


Solution

  • I am able to reproduce your situation:

    enter image description here

    This situation comes from the feature you are using:

    Top level statement

    This is a new feature start from C# 9.

    Why your solution makes the intellisense of VS disappear is because of two reasons:

    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 <Content>, 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.

    I suggest you to use common program's entry point instead of top level statement, so that you will be able to use intellisense and no error output while you writing code.

    For example:

    Program.cs:

    namespace ConsoleApp11
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                //Put the code here.
            }
        }
    }
    

    Samples/Program1.cs:

    namespace ConsoleApp11.Samples 
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                //Put the code here.
            }
        }
    }
    

    Please notice that in this situation, the usage still not correct, because there are multiple entry point, we just not encounter issue when writing.

    You still need to exclude the file in .csproj file like this if you want to run the app:

      <ItemGroup>
        <Compile Remove="Samples\Program1.cs" />
      </ItemGroup>
      <ItemGroup>
        <Content Include="Samples\Program1.cs" />
      </ItemGroup>
    

    Or use C# preprocessor directives such as:

    #if NEVER_COMPILE
    namespace ConsoleApp11.Samples
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                //Put the code here.
                Console.WriteLine("This is program1.");
            }
        }
    }
    #endif
    

    In Visual Studio, Intellisense and the compiler are tightly integrated. The Roslyn compiler, which is the .NET compiler platform, is responsible for both compiling code and providing code analysis services that power features like Intellisense. many intellisense issue of VS will also be report here:

    https://github.com/dotnet/roslyn/issues

    Since the Intellisense process relies on the list of files that are marked for compilation, you must make a choice. If you mark the file as compile by some means, you will encounter the error output. If you exclude the file from compile, you will lose mostly intellisense.

    By the way, you can also follow JonasH's suggestion, use unit test project in this situation. That will be more suitable.