0

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.

beppe9000
  • 1,056
  • 1
  • 13
  • 28
  • 1
    No. Typically you use a project (`.csproj)`, which effectively defines those options (and you combine it with `msbuild` or `dotnet`). There are also systems to create C# "scripts" (scriptcs and others), which use a special tool to compile and run your C# file in one go, and let you specify things like references inside the file. – canton7 Feb 15 '19 at 12:44
  • It would not be too hard to define a syntax for control comments, write the parameters in comments at the beginning of the file, extract and run them. – Axel Kemper Feb 15 '19 at 12:46
  • For your utilities, do you imagine using any sort of repository other than local or network filesystem or any delivery mechanism other than email? – Tom Blodget Feb 17 '19 at 14:06
  • You might consider PowerShell to be a .NET language that meets most of your requirements. [Example](https://stackoverflow.com/questions/3360867/add-reference-to-dll-in-powershell-2-0) with assembly reference. – Tom Blodget Feb 17 '19 at 19:22
  • @TomBlodget can PowerShell be compiled to exe or dll? – beppe9000 Feb 18 '19 at 13:07
  • No, they can only be referenced by other PowerShell scripts when designed for such a purpose. – Tom Blodget Feb 18 '19 at 15:24

1 Answers1

0

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.

beppe9000
  • 1,056
  • 1
  • 13
  • 28