我有一个对象
var testTcc = new TrendingConfigurationConfigDto
{
TrendingConfigurationId =1,
ConfigId = 1,
DeviceId = 1,
Selected = true,
YAxisPosition = YAxisPosition.Left,
Order = 1,
Color = "#ffffff",
Configuration = new BO.Shared.Dtos.List.ConfigurationListDto
{
Id = 1,
Name = "configuration",
Alias = "configuationAlias",
EnableEdit = true,
IsBusinessItem = true
},
Device = new BO.Shared.Dtos.List.DeviceListDto
{
Id = 1,
Name = "Device"
}
};
当我将它序列化为 json 时
var jsonTcc = SimpleJson.SerializeObject(testTcc);
它返回包含 YAxisPosition = 1 的 json 对象的字符串,当我尝试使用反序列化它时
testTcc = SimpleJson.DeserializeObject<TrendingConfigurationConfigDto>(jsonTcc);
它给出了异常 System.InvalidCastException 并显示消息“指定的转换无效”。
我尝试将 json 字符串中的 YAxisPosition 值更改为字符串“1”或“Left”,它总是给我相同的错误,直到我从 json 字符串中删除属性 YAxisPosition。
我可能会丢失一些东西(枚举属性的属性或类似的东西)。
请帮助我找到一种方法,以便我可以使用 RestSharp 序列化和反序列化包含 Enum 类型属性的对象。
注意:我尝试使用 NewtonSoft 成功进行序列化和反序列化。但我不希望我的 Web API 客户端依赖于 NetwonSoft,因为我已经在使用 RestSharp。