Building a method call

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 to provide it to the method Return. In C #, it looks like this:

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

and in Powershell I went about it this way (as suggested by @PetSerAl: Error returning with error ):

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

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

$something.Return($lambda)

however, I get this error

"" "1": " (: n = > new MyResultType {Foo = n.Bar}), ( : n = > new {Foo = n.Bar}), (: n = > n.Count()) (: n = > n.As(). Bar). (: n = > {var a = n + 1; return a; }) (: n = > new Foo ()). F #, . : " : 1 char: 1 + $neo.Cypher.Match( "n" ). ($ return) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: NotSpecified: (:) [], InvocationException + FullyQualifiedErrorId: ArgumentException

, -. - , ?

+1
1

C# :

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

, n => n.As<Project>():

Expression<Func<ICypherResultItem, Project>>

Expression Trees C#, - :

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)

, , C# , , , ... p >

* * , . var , MakeGenericMethod , :

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

All Articles