Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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.
@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; }"

hansschmucker commented Sep 4, 2014

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";^


hansschmucker commented Sep 4, 2014

Here's a link to the article explaining what this is good for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment