构建方法调用
Constructing a method call
尝试遵循本指南:https://github.com/Readify/Neo4jClient/wiki/cypher-examples#get-all-users-by-label
我需要创建一个 lambda 表达式,以便将其提供给 Return
方法。在 C# 中,它看起来像这样:
.Return(n => n.As<Project>())
在 Powershell 中,我已经这样做了(根据@PetSerAl 的建议:):
$exp = [System.Linq.Expressions.Expression]
$param = $exp::Parameter([Neo4jClient.Cyper.ICypherResultItem], "n")
$body = $exp::TypeAs($p, (new-object Project).GetType())
$lambda = $exp::Lambda([Func[Project]], $body, $p)
这样传递给 lambda 表达式的参数被键入以接收 Neo4j 表达式将传递的内容,并且方法的主体将其转换为 Project
(本地定义的 class)。现在我可以将它传递给我的方法:
$something.Return($lambda)
但是,我得到了这个错误
Exception calling "Return" with "1" argument(s): "The expression must
be constructed as either an object initializer (for example: n => new
MyResultType { Foo = n.Bar }), an anonymous type initializer (for
example: n => new { Foo = n.Bar }), a method call (for example: n =>
n.Count()), or a member accessor (for example: n => n.As().Bar). You
cannot supply blocks of code (for example: n => { var a = n + 1;
return a; }) or use constructors with arguments (for example: n => new
Foo(n)). If you're in F#, tuples are also supported. Parameter name:
expression" At line:1 char:1 + $neo.Cypher.Match("n").Return($return)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException +
FullyQualifiedErrorId : ArgumentException
这清楚地表明我没有正确制定 lambda 表达式的主体。谁能建议它应该如何代替?
在 C#
你有:
.Return(n => n.As<Project>())
如果我们把它拿出来看类型,n => n.As<Project>()
是:
Expression<Func<ICypherResultItem, Project>>
要在 C#
中使用 Expression Trees
创建它,我们最终会做如下事情:
ParameterExpression parameter = Expression.Parameter(typeof (ICypherResultItem), "n");
MethodCallExpression right = Expression.Call(parameter, typeof (ICypherResultItem).GetMethod("As").MakeGenericMethod(typeof(Project)));
Expression<Func<ICypherResultItem, Project>> expression = Expression.Lambda<Func<ICypherResultItem, Project>>(right, parameter);
因此,将其转换为 PowerShell 我认为它类似于:
$exp = [System.Linq.Expressions.Expression]
$param = $exp::Parameter([Neo4jClient.Cypher.ICypherResultItem], "n")
$body = $exp::Call($param, [Neo4jClient.Cypher.ICypherResultItem].GetMethod("As").MakeGenericMethod[Project])
$lambda = $exp::Lambda([Func[ICypherResultItem, Project]], $body, $param)
我 绝不是 一个 powershell 人,我怀疑你能更好地翻译 C#
,但希望这会让你进入正确的轨道...
* 更新我 *
一个小的修复,使它一切正常。声明一个 var 来保存 MakeGenericMethod
期望的类型数组,并将其传递给:
$PrjType = @((new-object Project).GetType())
$body = $exp::Call($param, [Neo4jClient.Cypher.ICypherResultItem].GetMethod("As").MakeGenericMethod($PrjType))
尝试遵循本指南:https://github.com/Readify/Neo4jClient/wiki/cypher-examples#get-all-users-by-label
我需要创建一个 lambda 表达式,以便将其提供给 Return
方法。在 C# 中,它看起来像这样:
.Return(n => n.As<Project>())
在 Powershell 中,我已经这样做了(根据@PetSerAl 的建议:
$exp = [System.Linq.Expressions.Expression]
$param = $exp::Parameter([Neo4jClient.Cyper.ICypherResultItem], "n")
$body = $exp::TypeAs($p, (new-object Project).GetType())
$lambda = $exp::Lambda([Func[Project]], $body, $p)
这样传递给 lambda 表达式的参数被键入以接收 Neo4j 表达式将传递的内容,并且方法的主体将其转换为 Project
(本地定义的 class)。现在我可以将它传递给我的方法:
$something.Return($lambda)
但是,我得到了这个错误
Exception calling "Return" with "1" argument(s): "The expression must be constructed as either an object initializer (for example: n => new MyResultType { Foo = n.Bar }), an anonymous type initializer (for example: n => new { Foo = n.Bar }), a method call (for example: n => n.Count()), or a member accessor (for example: n => n.As().Bar). You cannot supply blocks of code (for example: n => { var a = n + 1; return a; }) or use constructors with arguments (for example: n => new Foo(n)). If you're in F#, tuples are also supported. Parameter name: expression" At line:1 char:1 + $neo.Cypher.Match("n").Return($return) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException
这清楚地表明我没有正确制定 lambda 表达式的主体。谁能建议它应该如何代替?
在 C#
你有:
.Return(n => n.As<Project>())
如果我们把它拿出来看类型,n => n.As<Project>()
是:
Expression<Func<ICypherResultItem, Project>>
要在 C#
中使用 Expression Trees
创建它,我们最终会做如下事情:
ParameterExpression parameter = Expression.Parameter(typeof (ICypherResultItem), "n");
MethodCallExpression right = Expression.Call(parameter, typeof (ICypherResultItem).GetMethod("As").MakeGenericMethod(typeof(Project)));
Expression<Func<ICypherResultItem, Project>> expression = Expression.Lambda<Func<ICypherResultItem, Project>>(right, parameter);
因此,将其转换为 PowerShell 我认为它类似于:
$exp = [System.Linq.Expressions.Expression]
$param = $exp::Parameter([Neo4jClient.Cypher.ICypherResultItem], "n")
$body = $exp::Call($param, [Neo4jClient.Cypher.ICypherResultItem].GetMethod("As").MakeGenericMethod[Project])
$lambda = $exp::Lambda([Func[ICypherResultItem, Project]], $body, $param)
我 绝不是 一个 powershell 人,我怀疑你能更好地翻译 C#
,但希望这会让你进入正确的轨道...
* 更新我 *
一个小的修复,使它一切正常。声明一个 var 来保存 MakeGenericMethod
期望的类型数组,并将其传递给:
$PrjType = @((new-object Project).GetType())
$body = $exp::Call($param, [Neo4jClient.Cypher.ICypherResultItem].GetMethod("As").MakeGenericMethod($PrjType))