我有一个C#类,有价值物品。 我将这种财产归为一面价值,序列化到Json,然后将它重新注入物体。
How can I make the object s property value deserialise back to the enum?
这是因为:
public class Foo
{
public object Value { get; set; }
}
public enum SmallNumbers { One, Two, Three }
我怎样才能使这一检验顺从?
[Test]
public void an_object_property_set_to_an_enum_can_be_serialised()
{
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
};
var json = JsonConvert.SerializeObject(
new Foo {Value = SmallNumbers.One},
Formatting.None,
settings);
var foo = JsonConvert.DeserializeObject<Foo>(json, settings);
Assert.That(foo.Value is SmallNumbers);
}