简体   繁体   中英

How to specify compiler options in c# file?

I have a code.cs file that I compile with the following command line:

"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"

Is there a practical way to specify those options inside the fx1.cs file ?

This is mainly C#, but what about other .Net languages like Vb.Net and the others? Is there at least one language that can specify compiler parameters in a self-contained file?

Update: I also see there are answer files for csc, but they lack (or I can't see) enough flow control to embed one in a cs file.

The solution I came up with as baseline answer for this question is to turn the C# file into a "polyglot" *.cs.bat file that runs the full compile command on top of the C# source code.

/*? 2>NUL & @echo off
echo.
echo COMPILING...

"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs.bat ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"

PAUSE
GOTO:EOF REM */

// C# program...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

// blah...

To compile I can just run fx1.cs or fx1.cs.bat . That certinly is practical.

This may be useful but the downsides are at least these:

  • text highlighting is lost in the various editors that have it for *.cs files... If only there was a (easy) way to run *.cs files as batch ( type file | cmd seems to ignore PAUSE , EXIT /B , EXIT , GOTO:EOF )

  • The compiler path is hardcoded (but I think it can be determined with more batch fiddling)

  • It tries to run /*.exe , so the first line always gives me an error because I found no way to avoid it being run, so I can only put a ? in there (so it should not be a valid path ever) and hide it with error redirection.

While it is unpractical to edit in an IDE, I think this may be an easy format for distribution of simple utilities.

I'm not very good at this... so if someone has better ideas (for the polyglot route) please improve this answer by commenting or editing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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