我正在尝试在F#中执行某些操作,例如JsonConstructorAttribute示例> Json.NET文档:
I'm trying to do something in F# like the JsonConstructorAttribute example in the Json.NET documentation:
public class User { public string UserName { get; private set; } public bool Enabled { get; private set; } public User() { } [JsonConstructor] public User(string userName, bool enabled) { UserName = userName; Enabled = enabled; } }F#中的类似物似乎类似于以下内容,但在[<JsonConstructor>]的放置位置时出现错误:
The analog in F# appears to be something like the following, but I'm getting an error on the placement of [<JsonConstructor>]:
[<JsonConstructor>] // ERROR! (see below) type User (userName : string, enabled : bool) = member this.UserName = userName member this.Enabled = enabled错误是
此属性不适用于此语言元素
This attribute is not valid for use on this language element
那么,如何用[<JsonConstructor>]装饰主构造函数?
So then, how do I decorate the primary constructor with [<JsonConstructor>]?
推荐答案事实证明,这很简单.通过将[<JsonConstructor>]放在主要构造函数的参数列表之前来定位主要构造函数:
It turns out that it's pretty easy. Target the primary constructor by placing [<JsonConstructor>] just before the primary constructor's parameter list:
type User [<JsonConstructor>] (userName : string, enabled : bool) = member this.UserName = userName member this.Enabled = enabled更多推荐
F#:如何将Json.NET [JsonConstructor]属性应用于主要构造函数?
发布评论