Last active
August 29, 2015 13:57
-
-
Save cdhunt/9368874 to your computer and use it in GitHub Desktop.
Trying to create a variable containing a lambda expression
This file contains hidden or 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
# Trying to call ListByNameFiltered from | |
# http://www.orthogonal.com.au/computers/simpledb/index.htm | |
# (input parameters) => expression | |
# (id) => { return id == 1234; } | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Linq.Expressions") | |
if ( [string]::IsNullOrEmpty([psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get["Expression"]) ) | |
{ | |
[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::add("Expression", "System.Linq.Expressions.Expression") | |
} | |
[System.Linq.Expressions.ParameterExpression] $leftParam = [Expression]::Parameter([int], "id") | |
[System.Linq.Expressions.ConstantExpression] $rightParam = [Expression]::Constant(1234) | |
[System.Linq.Expressions.LabelTarget] $returnTarget = [Expression]::Label() | |
[System.Linq.Expressions.BinaryExpression] $equalExpression = [Expression]::Equal($leftParam, $rightParam) | |
# http://msdn.microsoft.com/en-us/library/dd402793(v=vs.110).aspx | |
[System.Linq.Expressions.GotoExpression] $returnEqual = [Expression]::Return($returnTarget, $equalExpression) | |
$expressionParams = [Linq.Expressions.ParameterExpression[]]@($leftParam, $rightParam) | |
$lambda = [Expression]::Lambda([func[int,int,int]], $returnEqual, $expressionParams) | |
<# | |
static System.Linq.Expressions.Expression[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression body, Params System.Linq.Expressions.ParameterExpression[] parameters) | |
static System.Linq.Expressions.LambdaExpression Lambda(type delegateType, System.Linq.Expressions.Expression body, Params System.Linq.Expressions.ParameterExpression[] parameters) | |
# http://msdn.microsoft.com/en-us/library/bb397951.aspx | |
Expression<Func<int, bool>> $lambda1 = | |
Expression.Lambda<Func<int, bool>>( | |
returnEqual, | |
new ParameterExpression[] { leftParam, rightParam }); | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment