@@ -20,9 +20,6 @@ protected override void Generate(SourceProductionContext spc)
20
20
HashSet < string > addedElemType = new HashSet < string > ( ) ;
21
21
foreach ( var type in typeSymbols )
22
22
{
23
- //obviously we cannot deserialize to an interface, we want an actual type
24
- if ( type . TypeKind == TypeKind . Interface ) continue ;
25
-
26
23
var typeFullName = type . ToDisplayString ( ) ;
27
24
if ( ! addedType . Add ( typeFullName ) ) continue ;
28
25
@@ -56,21 +53,37 @@ protected override void Generate(SourceProductionContext spc)
56
53
//if type implements IDictionary only
57
54
var idict = type . AllInterfaces . FirstOrDefault ( namedTypeSymbol =>
58
55
namedTypeSymbol . Name == "IDictionary" && namedTypeSymbol . TypeArguments . Length == 2 ) ;
56
+ if ( idict == null )
57
+ {
58
+ //if type is indeed IDictionary
59
+ idict = type . TypeKind == TypeKind . Interface && type . Name == "IDictionary"
60
+ ? ( INamedTypeSymbol ) type
61
+ : null ;
62
+ }
63
+
59
64
if ( idict != null )
60
65
{
61
66
if ( idict is { TypeArguments . Length : 2 } namedTypeSymbol )
62
67
{
63
68
var type1 = namedTypeSymbol . TypeArguments [ 0 ] . ToDisplayString ( ) ;
64
69
var type2 = namedTypeSymbol . TypeArguments [ 1 ] . ToDisplayString ( ) ;
65
- sb . AppendLine ( GenerateDictionarySerialization ( type1 , type2 , typeFullName , " " ) ) ;
70
+ sb . AppendLine ( GenerateDictionarySerialization ( type1 , type2 ,
71
+ namedTypeSymbol . TypeArguments [ 0 ] , namedTypeSymbol . TypeArguments [ 1 ] ,
72
+ typeFullName ,
73
+ type . TypeKind == TypeKind . Interface
74
+ ? "System.Collections.Generic.Dictionary<" + type1 + ", " + type2 + ">"
75
+ : typeFullName , " " ) ) ;
66
76
sb . GenerateClassDeserializeMethods ( typeFullName ) ;
77
+
67
78
continue ;
68
79
}
69
80
}
70
81
71
82
//if type is array
72
83
if ( type is IArrayTypeSymbol )
73
84
{
85
+ if ( ( ( IArrayTypeSymbol ) type ) . ElementType . IsUnmanagedType )
86
+ continue ;
74
87
var elemType = ( ( IArrayTypeSymbol ) type ) . ElementType . ToDisplayString ( ) ;
75
88
if ( addedElemType . Add ( elemType ) )
76
89
sb . AppendLine ( GenerateArraySerialization (
@@ -81,7 +94,8 @@ protected override void Generate(SourceProductionContext spc)
81
94
}
82
95
83
96
//ICollection<T>
84
- if ( type is INamedTypeSymbol { TypeArguments . Length : 1 } s )
97
+ if ( type is INamedTypeSymbol { TypeArguments . Length : 1 } s &&
98
+ s . AllInterfaces . Any ( namedTypeSymbol => namedTypeSymbol . Name == "ICollection" ) )
85
99
{
86
100
var elemType = s . TypeArguments [ 0 ] . ToDisplayString ( ) ;
87
101
if ( addedElemType . Add ( elemType ) && ! s . TypeArguments [ 0 ] . IsUnmanagedType )
@@ -183,12 +197,20 @@ public static void Deserialize(out {{elemType}}[] value, ref Reader reader)
183
197
return $ "{ indent } { ret } ";
184
198
}
185
199
186
- private static string GenerateDictionarySerialization ( string type1 , string type2 , string typeFullName ,
200
+ private static string GenerateDictionarySerialization ( string type1 , string type2 , ITypeSymbol keyType ,
201
+ ITypeSymbol valType ,
202
+ string sigTypeFullName ,
203
+ string typeFullName ,
187
204
string indent = "" )
188
205
{
206
+ bool isUnmanaged = keyType . IsUnmanagedType && valType . IsUnmanagedType ;
207
+ var reader = isUnmanaged ? "reader" : "eleReader" ;
208
+ var slice = isUnmanaged ? "" : "eleReader = reader.Slice();" ;
209
+ var eleReaderDecl = isUnmanaged ? "" : "Reader eleReader;" ;
210
+
189
211
var ret = $$ """
190
212
[MethodImpl(MethodImplOptions.AggressiveInlining)]
191
- public static void Deserialize(out {{ typeFullName }} value, ref Reader reader)
213
+ public static void Deserialize(out {{ sigTypeFullName }} value, ref Reader reader)
192
214
{
193
215
#if {{ NinoTypeHelper . WeakVersionToleranceSymbol }}
194
216
if (reader.Eof)
@@ -204,13 +226,13 @@ public static void Deserialize(out {{typeFullName}} value, ref Reader reader)
204
226
return;
205
227
}
206
228
207
- Reader eleReader;
229
+ {{ eleReaderDecl }}
208
230
209
231
value = new {{ typeFullName }} ({{ ( typeFullName . StartsWith ( "System.Collections.Generic.Dictionary" ) ? "length" : "" ) }} );
210
232
for (int i = 0; i < length; i++)
211
233
{
212
- eleReader = reader.Slice();
213
- Deserialize(out KeyValuePair<{{ type1 }} , {{ type2 }} > kvp, ref eleReader );
234
+ {{ slice }}
235
+ Deserialize(out KeyValuePair<{{ type1 }} , {{ type2 }} > kvp, ref {{ reader }} );
214
236
value[kvp.Key] = kvp.Value;
215
237
}
216
238
}
@@ -221,7 +243,8 @@ public static void Deserialize(out {{typeFullName}} value, ref Reader reader)
221
243
return $ "{ indent } { ret } ";
222
244
}
223
245
224
- private static string GenerateCollectionSerialization ( string prefix , string elemType , string sigTypeFullname ,
246
+ private static string GenerateCollectionSerialization ( string prefix , string elemType ,
247
+ string sigTypeFullname ,
225
248
string typeFullname ,
226
249
string indent )
227
250
{
@@ -253,7 +276,8 @@ public static void Deserialize(out {{sigTypeFullname}} value, ref Reader reader)
253
276
return $ "{ indent } { ret } ";
254
277
}
255
278
256
- private static string GenerateListSerialization ( string prefix , string elemType , string sigTypeFullname ,
279
+ private static string GenerateListSerialization ( string prefix , string elemType ,
280
+ string sigTypeFullname ,
257
281
string typeFullname ,
258
282
string indent ,
259
283
bool isInterface = false )
0 commit comments