Last active
August 29, 2015 13:56
-
-
Save altrive/9051839 to your computer and use it in GitHub Desktop.
Call C# code snipped from PowerShell using Mono.CSharp/Roslyn
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
Use-NuGetPackage Mono.CSharp -Verbose | |
#Setup Mono C# evaluater | |
$settings = New-Object Mono.CSharp.CompilerSettings | |
$printer = New-Object Mono.CSharp.ConsoleReportPrinter | |
$context = New-Object Mono.CSharp.CompilerContext($settings, $printer); | |
$evaluator = New-Object Mono.CSharp.Evaluator($context); | |
$evaluator.Run("using System; using System.Linq") > $null | |
#Define Func<int,int> return n^2 | |
$func = $evaluator.Evaluate('new Func<int,int>(p => {return p * p;})') | |
#Execute func | |
$func.Invoke(4) |
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
Use-NuGetPackage Roslyn -Verbose | |
#Setup Roslyn Script Engine | |
$scriptEngine = New-Object Roslyn.Scripting.CSharp.ScriptEngine($null, $null) | |
$scriptEngine.AddReference("System.Linq") | |
$scriptEngine.ImportNamespace("System") | |
$scriptEngine.ImportNamespace("System.Linq") | |
$session = $scriptEngine.CreateSession() | |
#Define Func<int,int> return n^2 | |
$func = $session.Execute('new Func<int,int>(p => {return p * p;})') | |
#Execute func | |
$func.Invoke(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment