在F#中创建的类在C#Web Api / MVC项目中错误地序列化

时间:2014-10-09 14:48:11

标签: asp.net-web-api f#

我在FSharp中创建了一个类,如下所示:

type Account() = class
    let mutable amount = 0m
    let mutable number = 0
    let mutable holder = ""

    member this.Number
        with get () = number
        and set (value) = number <- value

    member this.Holder
        with get () = holder
        and set (value) = holder <- value

    member this.Amount
        with get () = amount
        and set (value) = amount <- value

end

当我从像我这样的C#WebAPI / MVC应用程序中引用项目时

[HttpGet]
public Account Index()
{
    var account = new Account();
    account.Amount = 100;
    account.Holder = "Homer";
    account.Number = 1;
    return account;
}

我得到以下结果。请注意,字段名称是camelCased。

<Account xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ChickenSoftware.MVCSerializationIssue">
<amount>100</amount>
<holder>Homer</holder>
<number>1</number>
</Account>

当我在C#中创建类似的类

public class NewAccount
{
    public int Number { get; set; }
    public String Holder { get; set; }
    public int Amount { get; set; }
}

输出是Pascal cased

<NewAccount xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ChickenSoftware.MVCSerializationIssue.Api.Models">
<Amount>100</Amount>
<Holder>Homer</Holder>
<Number>1</Number>
</NewAccount>    

我首先想到的是Json Serializer(默认情况下我认为是Newtonsoft),但是当我在控制器中休息时,我看到C#类只公开了它的公共属性,并且F#类同时具有属性并暴露了支持领​​域。我试图添加一个&#34;私人&#34; F#let语句的关键字,但我得到了这个:

Error   1   Multiple visibility attributes have been specified for this identifier. 'let' bindings in classes are always private, as are any 'let' bindings inside expressions. 

那么F#类是否可以像Web Api一样对待C#?

1 个答案:

答案 0 :(得分:2)