/// <inheritdoc /> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Undefined) { // undefined 表示空的 Maybe<T> 实例。直接使用 Activator 实例化即可。 existingValue = Activator.CreateInstance(objectType); return(existingValue); } var valueType = MaybeUtils.GetInnerType(objectType); var value = serializer.Deserialize(reader, valueType); if (existingValue != null && MaybeUtils.IsMaybe(existingValue)) { MaybeUtils.SetInnerValue(existingValue, value); } else { existingValue = MaybeUtils.Create(valueType, value); } return(existingValue); }
public void TestCreate() { var maybe = (Maybe <int>)MaybeUtils.Create(10); Assert.AreEqual(10, maybe.Value); }
public void TestCreateNullValue() { var maybe = (Maybe <object>)MaybeUtils.Create(typeof(object), null); Assert.IsNull(maybe.Value); }
public void TestCreateNullValueWithoutType() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.Create(null)); }
public void TestCreateNullType() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.Create(null, new object())); }