我有一个对象。
当我将其序列化为JSON时,如下所示:
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进行序列化和反序列化。但是,由于我已经在使用RestSharp,所以我不想让我的Web API客户端依赖于NetwonSoft。