public void TestSetInnerValueNonNullableNull() { var maybe = new Maybe <int>(); var boxedMaybe = (object)maybe; Assert.Throws <InvalidOperationException>(() => MaybeUtils.SetInnerValue(boxedMaybe, null)); }
public void TestSetInnerValueNullValue() { var maybe = new Maybe <object>(); var boxedMaybe = (object)maybe; MaybeUtils.SetInnerValue(boxedMaybe, null); maybe = (Maybe <object>)boxedMaybe; Assert.IsNull(maybe.Value); }
public void TestSetInnerValue() { var maybe = new Maybe <int>(); var boxedMaybe = (object)maybe; MaybeUtils.SetInnerValue(boxedMaybe, 10); maybe = (Maybe <int>)boxedMaybe; Assert.AreEqual(10, maybe.Value); }
public void TestSetInnerValueDerived() { var maybe = new Maybe <IList <int> >(); var boxedMaybe = (object)maybe; var innerValue = new List <int>(); MaybeUtils.SetInnerValue(boxedMaybe, innerValue); maybe = (Maybe <IList <int> >)boxedMaybe; Assert.AreSame(innerValue, maybe.Value); }
/// <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 TestSetInnerValueTypeMismatch() { Assert.Throws <InvalidOperationException>(() => MaybeUtils.SetInnerValue(new Maybe <int>(), new object())); }
public void TestSetInnerValueNonMaybe() { Assert.Throws <ArgumentException>(() => MaybeUtils.SetInnerValue(new object(), new object())); }
public void TestSetInnerValueNull() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.SetInnerValue(null, 10)); Assert.DoesNotThrow(() => MaybeUtils.SetInnerValue(new Maybe <object>(), null)); }