在F#中访问动态属性

编程入门 行业动态 更新时间:2024-10-24 17:24:38
本文介绍了在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#中访问动态属性

本文发布于:2023-11-08 07:53:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!