Access dynamic property in F #

I tried to access dynamic property in Nancy. In Nancy, if the pass parameter in the request is a dynamic property. How can I access it.

There are many discussions / questions about this, but every time he first creates dynamics and then consumes it. How can I use what is already created?

Here are two pieces of code

public class ParameterModule : NancyModule { public ParameterModule():base("/{about}") { this.Get["/"] = (parameters) => "Hello About" + parameters.about; } } 

and for F #

 type ParameterModule() as this = inherit NancyModule("/{about}") do this.Get.["/"] <- fun parameters -> "Hello" + parameters?("about") :> obj 

I cannot access because the object does not have this property.

Please let me know if any further information is needed.

+4
source share
3 answers

The dynamic operator F # (?) Allows you to pass string parameters without the use of quotes, achieving the same syntax for dynamic C #, but you need to first define it for a specific use, the compiler just provides the syntax. Try the following:

 let (?) (parameters:obj) param = (parameters :?> Nancy.DynamicDictionary).[param] type ParameterModule() as this = inherit NancyModule("/{about}") do this.Get.["/"] <- fun parameters -> sprintf "Hello %O" parameters?about :> obj 
+9
source

I solved the problem by typecasting to dynamicdictionary. If there is a better way, let me know. Will keep the question open until then ...

Here is the code that solves the problem

 type ParameterModule() as this = inherit NancyModule("/{about}") do this.Get.["/"] <- fun parameters -> (parameters :?> Nancy.DynamicDictionary).["about"].ToString() :> obj 
+1
source

Maybe this can help you get started http://hubfs.net/topic/None/74053

+1
source

Source: https://habr.com/ru/post/1491318/


All Articles