@@ -21,7 +21,7 @@ internal static class DataSerializationHelper
21
21
#if NETFRAMEWORK
22
22
DataContractSurrogate = SerializationSurrogateProvider . Instance ,
23
23
#endif
24
- KnownTypes = [ typeof ( SurrogatedDateOnly ) , typeof ( SurrogatedTimeOnly ) ] ,
24
+ KnownTypes = [ typeof ( SurrogatedDateOnly ) , typeof ( SurrogatedTimeOnly ) , typeof ( SurrogatedSystemType ) ] ,
25
25
} ;
26
26
27
27
/// <summary>
@@ -163,6 +163,13 @@ private sealed class SurrogatedTimeOnly
163
163
public long Ticks { get ; set ; }
164
164
}
165
165
166
+ [ DataContract ]
167
+ private sealed class SurrogatedSystemType
168
+ {
169
+ [ DataMember ]
170
+ public string AssemblyQualifiedName { get ; set ; } = null ! ;
171
+ }
172
+
166
173
private sealed class SerializationSurrogateProvider
167
174
#if NETFRAMEWORK
168
175
: IDataContractSurrogate
@@ -190,20 +197,15 @@ public object GetDeserializedObject(object obj, Type targetType)
190
197
=> GetDeserializedObject ( obj ) ;
191
198
192
199
internal static object GetDeserializedObject ( object obj )
193
- {
194
- #if NET6_0_OR_GREATER
195
- if ( obj is SurrogatedDateOnly surrogatedDateOnly )
196
- {
197
- return DateOnly . FromDayNumber ( surrogatedDateOnly . DayNumber ) ;
198
- }
199
- else if ( obj is SurrogatedTimeOnly surrogatedTimeOnly )
200
+ => obj switch
200
201
{
201
- return new TimeOnly ( surrogatedTimeOnly . Ticks ) ;
202
- }
202
+ #if NET6_0_OR_GREATER
203
+ SurrogatedDateOnly surrogatedDateOnly => DateOnly . FromDayNumber ( surrogatedDateOnly . DayNumber ) ,
204
+ SurrogatedTimeOnly surrogatedTimeOnly => new TimeOnly ( surrogatedTimeOnly . Ticks ) ,
203
205
#endif
204
-
205
- return obj ;
206
- }
206
+ SurrogatedSystemType surrogatedSystemType => Type . GetType ( surrogatedSystemType . AssemblyQualifiedName ) ?? throw new SerializationException ( ) ,
207
+ _ => obj ,
208
+ } ;
207
209
208
210
public object GetObjectToSerialize ( object obj , Type targetType )
209
211
=> obj switch
@@ -212,6 +214,7 @@ public object GetObjectToSerialize(object obj, Type targetType)
212
214
DateOnly dateOnly => new SurrogatedDateOnly ( ) { DayNumber = dateOnly . DayNumber } ,
213
215
TimeOnly timeOnly => new SurrogatedTimeOnly ( ) { Ticks = timeOnly . Ticks } ,
214
216
#endif
217
+ Type type when type . AssemblyQualifiedName is { } typeAssemblyQualifiedName => new SurrogatedSystemType ( ) { AssemblyQualifiedName = typeAssemblyQualifiedName } ,
215
218
_ => obj ,
216
219
} ;
217
220
@@ -232,7 +235,7 @@ public Type GetSurrogateType(Type type)
232
235
}
233
236
#endif
234
237
235
- return type ;
238
+ return type . IsAssignableTo ( typeof ( Type ) ) ? typeof ( SurrogatedSystemType ) : type ;
236
239
}
237
240
}
238
241
#pragma warning restore IL3050 // IL3050: Avoid calling members annotated with 'RequiresDynamicCodeAttribute' when publishing as Native AOT
0 commit comments