From 99a7c698f0742470ed76158a57a812c58fcfa877 Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Tue, 14 Apr 2015 03:20:37 +0900 Subject: [PATCH] [S.R.Serialization] fix nullable serialization to match referencesource codegen. The original code is complicated enough that I misinterpreted the logic... The reulting code should look like this: DateTime? expr_94 = timeSubmitted; DateTime dateTime; bool arg_B5_0; if (XmlObjectSerializerWriteContext.GetHasValue (expr_94)) { dateTime = XmlObjectSerializerWriteContext.GetNullableValue (expr_94); arg_B5_0 = false; } else { dateTime = XmlObjectSerializerWriteContext.GetDefaultValue (); arg_B5_0 = true; } The fixed code should be like this now. --- .../XmlFormatReaderGenerator_static.cs | 2 +- .../XmlFormatWriterGenerator_static.cs | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatReaderGenerator_static.cs b/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatReaderGenerator_static.cs index 1f2c4847499..8b81beca399 100644 --- a/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatReaderGenerator_static.cs +++ b/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatReaderGenerator_static.cs @@ -300,7 +300,7 @@ namespace System.Runtime.Serialization if (objectId == Globals.NullObjectId) { if (nullables != 0) - value = FormatterServices.GetUninitializedObject (valueType); + value = Activator.CreateInstance (valueType); else if (type.IsValueType) throw new SerializationException (SR.GetString (SR.ValueTypeCannotBeNull, DataContract.GetClrTypeFullName (type))); else diff --git a/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatWriterGenerator_static.cs b/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatWriterGenerator_static.cs index 9034ea31d6d..7cd200c0498 100644 --- a/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatWriterGenerator_static.cs +++ b/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlFormatWriterGenerator_static.cs @@ -462,9 +462,7 @@ namespace System.Runtime.Serialization { bool isNull; if (isNullableOfT) - { memberValue = UnwrapNullableObject(() => memberValue, ref memberType, out isNull); //Leaves !HasValue on stack - } else isNull = memberValue == null; if (isNull) @@ -499,26 +497,28 @@ namespace System.Runtime.Serialization void InternalSerialize (MethodInfo methodInfo, Func memberValue, Type memberType, bool writeXsiType) { - var typeHandleValue = Type.GetTypeHandle (memberValue); - var isDeclaredType = typeHandleValue.Equals (CodeInterpreter.ConvertValue (memberValue, memberType, Globals.TypeOfObject)); - methodInfo.Invoke (ctx, new object [] {writer, memberValue != null ? memberValue () : null, isDeclaredType, writeXsiType, DataContract.GetId (memberType.TypeHandle), memberType.TypeHandle}); + var v = memberValue (); + var typeHandleValue = Type.GetTypeHandle (v); + var isDeclaredType = typeHandleValue.Equals (CodeInterpreter.ConvertValue (v, memberType, Globals.TypeOfObject)); + methodInfo.Invoke (ctx, new object [] {writer, memberValue != null ? v : null, isDeclaredType, writeXsiType, DataContract.GetId (memberType.TypeHandle), memberType.TypeHandle}); } object UnwrapNullableObject(Func memberValue, ref Type memberType, out bool isNull)// Leaves !HasValue on stack { - object nullableUnwrappedMemberValue = null; + object v = memberValue (); isNull = false; while (memberType.IsGenericType && memberType.GetGenericTypeDefinition () == Globals.TypeOfNullable) { Type innerType = memberType.GetGenericArguments () [0]; - if ((bool) XmlFormatGeneratorStatics.GetHasValueMethod.MakeGenericMethod (innerType).Invoke (innerType, new object [0]) == null) { + if ((bool) XmlFormatGeneratorStatics.GetHasValueMethod.MakeGenericMethod (innerType).Invoke (null, new object [] {v})) + v = XmlFormatGeneratorStatics.GetNullableValueMethod.MakeGenericMethod (innerType).Invoke (null, new object [] {v}); + else { isNull = true; - return XmlFormatGeneratorStatics.GetDefaultValueMethod.MakeGenericMethod (memberType).Invoke (null, new object [0]); + v = XmlFormatGeneratorStatics.GetDefaultValueMethod.MakeGenericMethod (memberType).Invoke (null, new object [0]); } - nullableUnwrappedMemberValue = XmlFormatGeneratorStatics.GetNullableValueMethod.MakeGenericMethod (innerType).Invoke (memberValue (), new object [0]); memberType = innerType; } - - return memberValue; + + return v; } bool TryWritePrimitive(Type type, Func value, MemberInfo memberInfo, int? arrayItemIndex, XmlDictionaryString ns, XmlDictionaryString name, int nameIndex) -- 2.25.1