6
6
using System ;
7
7
using System . Collections . Generic ;
8
8
using System . Linq ;
9
+ using System . Text ;
9
10
using Microsoft . CodeAnalysis ;
10
11
11
12
namespace MessagePackCompiler . CodeAnalysis
@@ -269,6 +270,7 @@ public class TypeCollector
269
270
private List < EnumSerializationInfo > collectedEnumInfo ;
270
271
private List < GenericSerializationInfo > collectedGenericInfo ;
271
272
private List < UnionSerializationInfo > collectedUnionInfo ;
273
+ private List < ObjectSerializationInfo > collectedUnboundGenericInfo ;
272
274
273
275
public TypeCollector ( Compilation compilation , bool disallowInternal , bool isForceUseMap , Action < string > logger )
274
276
{
@@ -307,10 +309,11 @@ private void ResetWorkspace()
307
309
this . collectedEnumInfo = new List < EnumSerializationInfo > ( ) ;
308
310
this . collectedGenericInfo = new List < GenericSerializationInfo > ( ) ;
309
311
this . collectedUnionInfo = new List < UnionSerializationInfo > ( ) ;
312
+ this . collectedUnboundGenericInfo = new List < ObjectSerializationInfo > ( ) ;
310
313
}
311
314
312
315
// EntryPoint
313
- public ( ObjectSerializationInfo [ ] objectInfo , EnumSerializationInfo [ ] enumInfo , GenericSerializationInfo [ ] genericInfo , UnionSerializationInfo [ ] unionInfo ) Collect ( )
316
+ public ( ObjectSerializationInfo [ ] objectInfo , EnumSerializationInfo [ ] enumInfo , GenericSerializationInfo [ ] genericInfo , UnionSerializationInfo [ ] unionInfo , ObjectSerializationInfo [ ] unboundGenericInfo ) Collect ( )
314
317
{
315
318
this . ResetWorkspace ( ) ;
316
319
@@ -323,7 +326,8 @@ private void ResetWorkspace()
323
326
this . collectedObjectInfo . OrderBy ( x => x . FullName ) . ToArray ( ) ,
324
327
this . collectedEnumInfo . OrderBy ( x => x . FullName ) . ToArray ( ) ,
325
328
this . collectedGenericInfo . Distinct ( ) . OrderBy ( x => x . FullName ) . ToArray ( ) ,
326
- this . collectedUnionInfo . OrderBy ( x => x . FullName ) . ToArray ( ) ) ;
329
+ this . collectedUnionInfo . OrderBy ( x => x . FullName ) . ToArray ( ) ,
330
+ this . collectedUnboundGenericInfo . OrderBy ( x => x . FullName ) . ToArray ( ) ) ;
327
331
}
328
332
329
333
// Gate of recursive collect
@@ -533,10 +537,49 @@ private void CollectGeneric(INamedTypeSymbol type)
533
537
534
538
this . collectedGenericInfo . Add ( enumerableInfo ) ;
535
539
}
540
+
541
+ return ;
542
+ }
543
+
544
+ if ( type . IsDefinition )
545
+ {
546
+ ObjectSerializationInfo unboundGenericInfo = GetObjectInfo ( type ) ;
547
+ collectedUnboundGenericInfo . Add ( unboundGenericInfo ) ;
548
+ }
549
+ else
550
+ {
551
+ foreach ( ITypeSymbol item in type . TypeArguments )
552
+ {
553
+ this . CollectCore ( item ) ;
554
+ }
555
+
556
+ StringBuilder formatterBuilder = new StringBuilder ( ) ;
557
+ if ( ! type . ContainingNamespace . IsGlobalNamespace )
558
+ {
559
+ formatterBuilder . Append ( type . ContainingNamespace . ToDisplayString ( ) + "." ) ;
560
+ }
561
+
562
+ formatterBuilder . Append ( type . Name + "Formatter" ) ;
563
+ formatterBuilder . Append ( "<" + string . Join ( ", " , type . TypeArguments ) + ">" ) ;
564
+
565
+ GenericSerializationInfo info = new GenericSerializationInfo
566
+ {
567
+ FullName = type . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ,
568
+
569
+ FormatterName = formatterBuilder . ToString ( ) ,
570
+ } ;
571
+
572
+ this . collectedGenericInfo . Add ( info ) ;
536
573
}
537
574
}
538
575
539
576
private void CollectObject ( INamedTypeSymbol type )
577
+ {
578
+ ObjectSerializationInfo info = GetObjectInfo ( type ) ;
579
+ collectedObjectInfo . Add ( info ) ;
580
+ }
581
+
582
+ private ObjectSerializationInfo GetObjectInfo ( INamedTypeSymbol type )
540
583
{
541
584
var isClass = ! type . IsValueType ;
542
585
@@ -929,20 +972,32 @@ private void CollectObject(INamedTypeSymbol type)
929
972
needsCastOnAfter = ! type . GetMembers ( "OnAfterDeserialize" ) . Any ( ) ;
930
973
}
931
974
975
+ string templateParametersString ;
976
+ if ( type . TypeParameters . Count ( ) > 0 )
977
+ {
978
+ templateParametersString = "<" + string . Join ( ", " , type . TypeParameters ) + ">" ;
979
+ }
980
+ else
981
+ {
982
+ templateParametersString = null ;
983
+ }
984
+
932
985
var info = new ObjectSerializationInfo
933
986
{
934
987
IsClass = isClass ,
935
988
ConstructorParameters = constructorParameters . ToArray ( ) ,
936
989
IsIntKey = isIntKey ,
937
990
Members = isIntKey ? intMembers . Values . ToArray ( ) : stringMembers . Values . ToArray ( ) ,
938
991
Name = type . ToDisplayString ( ShortTypeNameFormat ) . Replace ( "." , "_" ) ,
992
+ TemplateParametersString = templateParametersString ,
939
993
FullName = type . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ,
940
994
Namespace = type . ContainingNamespace . IsGlobalNamespace ? null : type . ContainingNamespace . ToDisplayString ( ) ,
941
995
HasIMessagePackSerializationCallbackReceiver = hasSerializationConstructor ,
942
996
NeedsCastOnAfter = needsCastOnAfter ,
943
997
NeedsCastOnBefore = needsCastOnBefore ,
944
998
} ;
945
- this . collectedObjectInfo . Add ( info ) ;
999
+
1000
+ return info ;
946
1001
}
947
1002
948
1003
private static bool TryGetNextConstructor ( IEnumerator < IMethodSymbol > ctorEnumerator , ref IMethodSymbol ctor )
0 commit comments