PoshCode Archive  Artifact [748326c49c]

Artifact 748326c49c9c94fb4e9a75d942ff4c14bcef70ff114c4ab0e0f586d2465fc373:

  • File POC-csharp-expressions.ps1 — part of check-in [2fb850be3d] at 2018-06-10 14:25:14 on branch trunk — proof of concept of running csharp expressions in powershell v2. very basic, no error checking. (user: unknown size: 1266)

# encoding: ascii
# api: csharp
# title: POC csharp expressions
# description: proof of concept of running csharp expressions in powershell v2. very basic, no error checking. 
# version: 0.1
# type: function
# license: CC0
# function: run-csharpexpression
# x-poshcode-id: 853
# x-derived-from-id: 856
# x-archived: 2009-02-08T15:01:56
#
# -karl prosser
#
function run-csharpexpression([string] $expression )
{
$local:name  =  [system.guid]::NewGuid().tostring().replace('-','_').insert(0,"csharpexpr")
$local:template = @"
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace ShellTools.DynamicCsharpExpression
{
    public class [[CLASSNAME]]
    {
        public static object RunExpression()
        {
            return [[EXPRESSION]];
        }
    }
}
"@
$local:source = $local:template.replace("[[CLASSNAME]]",$local:name).replace("[[EXPRESSION]]",$expression)

add-Type $local:source -Language CsharpVersion3 | out-Null
 invoke-Expression $('[ShellTools.DynamicCsharpExpression.' + $local:name + ']::RunExpression()' )
}
set-alias c run-csharpexpression 
c DateTime.Now
c "new{a=1,b=2,c=3}"
c 'from x in Directory.GetFiles(@"c:\downloads") where x.Contains("win") select x'