@@ -91,9 +91,7 @@ internal static MemberTypeInfo Decode(BinaryReader reader, int count, PayloadOpt
9191 const AllowedRecordTypes SystemClass = Classes | AllowedRecordTypes . SystemClassWithMembersAndTypes
9292 // All primitive types can be stored by using one of the interfaces they implement.
9393 // Example: `new IEnumerable[1] { "hello" }` or `new IComparable[1] { int.MaxValue }`.
94- | AllowedRecordTypes . BinaryObjectString | AllowedRecordTypes . MemberPrimitiveTyped
95- // System.Nullable<UserStruct> is a special case of SystemClassWithMembersAndTypes
96- | AllowedRecordTypes . ClassWithMembersAndTypes ;
94+ | AllowedRecordTypes . BinaryObjectString | AllowedRecordTypes . MemberPrimitiveTyped ;
9795 const AllowedRecordTypes NonSystemClass = Classes | AllowedRecordTypes . ClassWithMembersAndTypes ;
9896
9997 return binaryType switch
@@ -104,10 +102,29 @@ internal static MemberTypeInfo Decode(BinaryReader reader, int count, PayloadOpt
104102 BinaryType . StringArray => ( StringArray , default ) ,
105103 BinaryType . PrimitiveArray => ( PrimitiveArray , default ) ,
106104 BinaryType . Class => ( NonSystemClass , default ) ,
105+ BinaryType . SystemClass when IsNullableUserTypeRepresentedAsSystemType ( ( TypeName ) additionalInfo ! ) => ( SystemClass | AllowedRecordTypes . ClassWithMembersAndTypes , default ) ,
107106 BinaryType . SystemClass => ( SystemClass , default ) ,
108107 BinaryType . ObjectArray => ( ObjectArray , default ) ,
109108 _ => throw new InvalidOperationException ( )
110109 } ;
110+
111+ static bool IsNullableUserTypeRepresentedAsSystemType ( TypeName typeName )
112+ {
113+ if ( ! typeName . IsConstructedGenericType || typeName . Name != typeof ( Nullable < > ) . Name )
114+ {
115+ return false ;
116+ }
117+
118+ var genericArgs = typeName . GetGenericArguments ( ) ;
119+ if ( genericArgs . Length != 1 )
120+ {
121+ return false ;
122+ }
123+
124+ // If the generic argument is not from CoreLib, then it is a user type.
125+ var assemblyName = genericArgs [ 0 ] . AssemblyName ;
126+ return assemblyName is not null && ! assemblyName . FullName . Equals ( TypeNameHelpers . CoreLibAssemblyName . FullName , StringComparison . Ordinal ) ;
127+ }
111128 }
112129
113130 internal TypeName GetArrayTypeName ( ArrayInfo arrayInfo )
0 commit comments