Constructing a method call

為{幸葍}努か 提交于 2019-12-01 09:37:36
Chris Skardon

In C# you have:

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

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

Expression<Func<ICypherResultItem, Project>>

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

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)

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...

* 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))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!