How to compile just one file in c#?

前端 未结 7 1184
星月不相逢
星月不相逢 2020-12-28 15:54

In VC++ I can press CTRL+F7 to compile a single file, or right click on a source file ot compile it. Is it possible to compile a single file (or current file) in C#?

相关标签:
7条回答
  • 2020-12-28 16:23

    No it is not possible to do this in C#.

    Unlike C++ a C# file cannot be reasonably compiled on it's own to determine if it has any errors. C++ achieves this through #include statements which allows a .cpp file to understand the declaration of types available. These declarations define the structure of types the current file depends on and allows the compiler to ensure they are used according to specification.

    This process is handled implicitly in C#. The set of declarations available is simply the set of all declarations in all compiled files which are otherwise accessible. There is no way to forward declare dependencies in the manner C++ does and hence no way to ensure they are being used correctly within a single file.

    0 讨论(0)
  • 2020-12-28 16:32

    A Visual Studio add-in tool like ReSharper is a very good investment for this situation.

    ReSharper performs continuous background solution-wide code analysis and will report issues by conveniently displaying a bar next to your code file\document scrollbar which has red\orange lines denoting any lines of code that have issues\errors. The displayed lines are click-able to navigate to the line in question and also have tool-tips detailing what the exact problem is:

    http://www.jetbrains.com/resharper/features/code_analysis.html#Continuous_Code_Quality_Analysis

               http://www.jetbrains.com/resharper/features/screenshots/50/marker_bar.png
    

    The issues\warnings that ReSharper can check for are configurable (but it has excellent configuration out-of-the-box), and can denote anything from errors which would cause the code not to compile to more subtle issues where it has detected a possible null method call result which has not been explicitly checked for.

    0 讨论(0)
  • 2020-12-28 16:33

    Yes, this can be done using the Mono .NET Framework. At the command prompt, run mcs path/to/file.cs.

    From the Mono docs:

    To compile, use csc:

    csc hello.cs
    

    Note: csc compiler is not available on all platforms or in very old Mono versions, in such cases use mcs instead.

    The compiler will create “hello.exe”, which you can run using:

    mono hello.exe
    
    0 讨论(0)
  • 2020-12-28 16:36

    In command line: %windir%\Microsoft.Net\framework\V3.5\csc.exe /target:library File.cs

    You could reasonably attach this to the solution explorers context menu through Tools->External Tools

    set the arguments to /target:library $(ItemPath)

    something like that might do what you want. Though the file would have to depend on no other files in the project or in referenced binaries aside from what's in the GAC.

    0 讨论(0)
  • 2020-12-28 16:37

    Yes it's possible. You can call the compiler directly using command prompt. i.e.

    1. Create single file 'hello.cs'
    2. Open the Visual Studio command prompt
    3. Navigate to the directory that has 'hello.cs'
    4. Run csc hello.cs
    5. Execute your single file by typing hello.exe

    This will at least tell you whether a single file compiles or not. You can find more information here: https://msdn.microsoft.com/en-us/library/78f4aasd.aspx

    0 讨论(0)
  • 2020-12-28 16:39

    For single .cs file compile + run:

    1. In VS 2008, go to "Tools" > "External Tools"
    2. Click on "Add"
    3. Title: Run CSC (or whatever you want)
    4. Command: C:\Windows\system32\cmd.exe
    5. Arguments: /c C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:winexe $(ItemPath) && $(ItemFileName)
    6. Initial directory: $(ItemDir)
    7. Check Use Output Window
    8. Apply + Ok
    9. Go to Tools and choose "Run CSC"

    If this doesn't work, verify that your paths for cmd and csc match.

    0 讨论(0)
提交回复
热议问题