本文介绍了F#:如何将Json.NET [JsonConstructor]属性应用于主要构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在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
错误是
那么,如何用[<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]属性应用于主要构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!