Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing dynamic property in F#

I was trying to access dynamic property in Nancy. In Nancy if pass parameter in query it comes as dynamic property. How can I access that.

There are many discussion / question about this but every where, first it is of creating dynamic and then consuming it. How can I consume what is already created?

Here are two code snippet

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 can't access about as object don't have that property.

Please let me know if any further information needed.

like image 516
kunjee Avatar asked Dec 16 '22 08:12

kunjee


1 Answers

The F# dynamic operator (?) allows you to pass string parameters without using the quotes, achieving a similar syntax to C# dynamic, but you need to define it first for your concrete use case, the compiler just provides the syntax. Try this:

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
like image 168
Gustavo Guerra Avatar answered Jan 03 '23 07:01

Gustavo Guerra