在F#中访问动态属性 [英] Accessing dynamic property in F#

查看:39
本文介绍了在F#中访问动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问Nancy中的动态属性.在Nancy中,如果查询中的传递参数为动态属性.我该如何访问.

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?

这是两个代码段

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

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

推荐答案

F#动态运算符(?)允许您在不使用引号的情况下传递字符串参数,从而实现了与C#dynamic类似的语法,但是您需要首先对其进行定义对于您的具体用例,编译器仅提供语法.试试这个:

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

这篇关于在F#中访问动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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