有没有办法将Json.Net反序列化与不可变的类一起使用? [英] Is there a way to use Json.Net deserialization with immutable classes?

查看:65
本文介绍了有没有办法将Json.Net反序列化与不可变的类一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用json的API.我已经创建了一些用于对API建模的类.为了使生活更轻松,我的模型使用了公共属性,当将json反序列化为对象时,Json.Net会使用这些公共属性.

I'm working with an API that uses json. I have some classes that I've created to model the API. To make life easy, my models use public properties, which are in turn used by Json.Net when deserializing the json into objects.

我想使对象不可变,但是我遇到了一个问题,因为如果我将属性设置为只读,则会破坏反序列化.我有办法拥有不可变的对象并使用反序列化吗?

I'd like to make my objects immutable, but I'm running into a problem because if I make my properties read only, I break the deserialization. Is there a way for me to have immutable objects, and use deserialization?

推荐答案

我认为您应该可以使用此问题.

I think you should be able to use JsonConstructorAttribute. See this question for an example.

这篇关于有没有办法将Json.Net反序列化与不可变的类一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆