Skip to content

Instantly share code, notes, and snippets.

@hansschmucker hansschmucker/c#.cmd
Last active Aug 29, 2015

Embed
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

This comment has been minimized.

Copy link
Owner Author

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";^
System.Console.WriteLine(a);^
|c#

@hansschmucker

This comment has been minimized.

Copy link
Owner Author

hansschmucker commented Sep 4, 2014

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.