Return overload fails

后端 未结 4 1334
独厮守ぢ
独厮守ぢ 2021-01-20 01:34

I\'m following this little write up: https://github.com/Readify/Neo4jClient/wiki/cypher but I\'m doing it from Powershell. so what I have so far is

[System.         


        
4条回答
  •  [愿得一人]
    2021-01-20 02:09

    There are two problems with Return method:

    1. It accepts argument of expression tree type, rather then compiled delegate type. And PowerShell does not have an easy way to create expression tree from a ScriptBlock. So, you have to create expression tree by hands or use string overload.
    2. string overload does not allows PowerShell to infer generic parameter for method, and PowerShell syntax does not allows to specifying generic parameter explicitly. So, PowerShell can not call string overload of Return method directly. You have to use some workaround to call it, for example, call it thru Reflection.

    Sample how can you create a simple expression tree ((a,b) => a*2+b) in PowerShell:

    # First way: messing with [System.Linq.Expressions.Expression]
    $a=[System.Linq.Expressions.Expression]::Parameter([int],'a')
    $b=[System.Linq.Expressions.Expression]::Parameter([int],'b')
    $2=[System.Linq.Expressions.Expression]::Constant(2)
    
    $Body=[System.Linq.Expressions.Expression]::Add([System.Linq.Expressions.Expression]::Multiply($a,$2),$b)
    $Sum=[System.Linq.Expressions.Expression]::Lambda([Func[int,int,int]],$Body,$a,$b)
    $Sum
    
    # Second way: using help of C#
    Add-Type -TypeDefinition @'
        using System;
        using System.Linq.Expressions;
        public static class MyExpression {
            public static readonly Expression> Sum=(a,b) => a*2+b;
        }
    '@
    [MyExpression]::Sum
    

提交回复
热议问题