Last active
August 29, 2015 14:06
-
-
Save hansschmucker/820d3bdeca34c665b77a to your computer and use it in GitHub Desktop.
Compiles C# code piped to it and runs it immediately without a temporary EXE. Prefix "@" if you want to describe a whole class, not just the content of main.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PowerShell -command "$src=''+$input; if ( $src -match '^[\s]*@([\s\S]*)$' ){ $src=$matches[1]; }else{ $src='using System; public class App { public static void Main() { '+$src+' } } '; } $opt = New-Object System.CodeDom.Compiler.CompilerParameters; $opt.GenerateInMemory = $true; $cr = [System.CodeDom.Compiler.CodeDomProvider]::CreateProvider('CSharp').CompileAssemblyFromSource($opt, $src); if($cr.CompiledAssembly) {$obj = $cr.CompiledAssembly.CreateInstance('App'); $obj.GetType().GetMethod('Main').Invoke($obj, $null);}else{ $cr.errors; }" |
Here's a link to the article explaining what this is good for:
http://www.tapper-ware.net/blog/?p=349
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use it, you can just pipe code to it (just be sure to escape the linebreak and adding a space before the final pipe character):
@echo ^
var a="Hello World";^
System.Console.WriteLine(a);^
|c#