在F#创建的类在C#的WebAPI / MVC项目序列错误 [英] Classes created in F# serialized incorrectly in a C# WebApi/MVC project
问题描述
我创建了一个类FSharp像这样:
I created a class in FSharp like so:
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应用程序像这样
When I reference the project from my C# WebAPI/MVC application like this
[HttpGet]
public Account Index()
{
var account = new Account();
account.Amount = 100;
account.Holder = "Homer";
account.Number = 1;
return account;
}
我得到下面的结果。请注意,字段名是驼峰格式。
I am getting the following results. Note that the field name are 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#这样的同级
When I create a similar class in C# like this
public class NewAccount
{
public int Number { get; set; }
public String Holder { get; set; }
public int Amount { get; set; }
}
输出帕斯卡尔套管
the output is 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序列化(默认情况下,我认为Newtonsoft),但是当我把休息控制器,我看到C#类只有它的公共属性公开,而且F#类有两个属性并支持字段曝光。我尝试添加一个私人关键字的F#让报表,但我得到了这一点:
I first thought it was the Json Serializer (Newtonsoft by default I think), but when I put a break in the controller, I see that the C# class only has its public properties exposed and that the F# class has both the Property and the backing field exposed. I tried to add a "private" keyword to the F# let statements but I got this:
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#类可以视为与C#从网页API?
So is there a way that the F# classes can treated the same as C# from Web Api?
推荐答案
请参阅上就此问题马克的博客:的http://blog.ploeh.dk/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records/
See Mark's blog on this very issue: http://blog.ploeh.dk/2013/10/15/easy-aspnet-web-api-dtos-with-f-climutable-records/
这篇关于在F#创建的类在C#的WebAPI / MVC项目序列错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!