构造方法调用 [英] Constructing a method call

查看:44
本文介绍了构造方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试遵循本指南:https://github.com/Readify/Neo4jClient/wiki/cypher-examples#get-all-users-by-label我需要创建一个 lambda 表达式,以便将它提供给 Return 方法.在 C# 中,它看起来像这样:

in trying to follow this guide: https://github.com/Readify/Neo4jClient/wiki/cypher-examples#get-all-users-by-label I need to create a lambda expression in order to provide it to the Return method. In C# it looks like this:

.Return(n => n.As<Project>())

并且在 Powershell 中我已经这样做了(根据@PetSerAl 的建议:返回重载失败):

and in Powershell I've gone about it this way (as per @PetSerAl's suggestion: Return overload fails):

$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(本地定义的类).现在我可以将它传递给我的方法:

such that the parameter passed to the lambda expression is typed to receive what the Neo4j expression will pass and the body of the method converts it to a Project (a locally defined class). now I can pass it to my method:

$something.Return($lambda)

但是,我收到此错误

使用1"个参数调用Return"的异常:表达式必须构造为对象初始值设定项(例如:n => newMyResultType { Foo = n.Bar }),一个匿名类型初始值设定项(用于例如:n => new { Foo = n.Bar }),一个方法调用(例如:n =>n.Count()) 或成员访问器(例如:n => n.As().Bar).你不能提供代码块(例如:n => { var a = n + 1;返回一个;}) 或使用带参数的构造函数(例如:n => newFoo(n)).如果您使用 F#,还支持元组.参数名称:表达式" 在行:1 字符:1 + $neo.Cypher.Match("n").Return($return)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [],方法调用异常 +FullQualifiedErrorId : ArgumentException

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 表达式的主体.任何人都可以建议它应该如何?

which makes it clear I didn't formulate the body of the lambda expression correctly. can anyone suggest how it should be instead?

推荐答案

C# 中你有:

.Return(n => n.As<Project>())

如果我们把它拿出来看看类型,n =>n.As() 是:

If we take that out and look at the type, n => n.As<Project>() is:

Expression<Func<ICypherResultItem, Project>>

要使用 C# 中的 Expression Trees 来创建它,我们最终会执行以下操作:

To create that using Expression Trees in C#, we end up doing something like:

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 我认为类似于:

So, converting that to PowerShell I think it's something like:

$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)

绝不是一个强大的人,我怀疑你能够更好地翻译C#,但希望这能让你走上正轨...

I am in no way a powershell person, and I suspect you'll be able to translate the C# better, but hopefully this will get you onto the right track...

* 更新我 *一个小修复,让它一切正常.声明一个 var 来保存 MakeGenericMethod 期望的类型数组,并将其传入:

* Update I * a small fix that makes it all work. declare a var to hold the array of types that MakeGenericMethod expects, and pass that in:

$PrjType = @((new-object Project).GetType())
$body = $exp::Call($param, [Neo4jClient.Cypher.ICypherResultItem].GetMethod("As").MakeGenericMethod($PrjType))

这篇关于构造方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆