@@ -33,6 +33,10 @@ internal void Emit(ModelReaderWriterContextGenerationSpec contextGenerationSpec)
3333
3434 int indent = 0 ;
3535 StringBuilder builder = new ( ) ;
36+
37+ builder . AppendLine ( "// <auto-generated/>" ) ;
38+ builder . AppendLine ( ) ;
39+
3640 builder . AppendLine ( indent , "#nullable disable" ) ;
3741 builder . AppendLine ( ) ;
3842
@@ -79,6 +83,7 @@ internal void Emit(ModelReaderWriterContextGenerationSpec contextGenerationSpec)
7983 builder . AppendLine ( ) ;
8084
8185 builder . AppendLine ( indent , $ "private static { contextName } _{ contextName . ToCamelCase ( ) } ;") ;
86+ builder . AppendLine ( indent , "/// <summary> Gets the default instance </summary>" ) ;
8287 builder . AppendLine ( indent , $ "public static { contextName } Default => _{ contextName . ToCamelCase ( ) } ??= new();") ;
8388 builder . AppendLine ( ) ;
8489
@@ -106,6 +111,7 @@ internal void Emit(ModelReaderWriterContextGenerationSpec contextGenerationSpec)
106111 builder . AppendLine ( ) ;
107112 }
108113
114+ builder . AppendLine ( indent , "/// <inheritdoc/>" ) ;
109115 builder . AppendLine ( indent , "protected override bool TryGetTypeBuilderCore(Type type, out ModelReaderWriterTypeBuilder builder)" ) ;
110116 builder . AppendLine ( indent , "{" ) ;
111117 indent ++ ;
@@ -161,7 +167,7 @@ private static bool ShouldGenerateAsLocal(ModelReaderWriterContextGenerationSpec
161167 return modelInfo . Kind == TypeBuilderKind . Array ||
162168 modelInfo . Kind == TypeBuilderKind . MultiDimensionalArray ||
163169 ! referenceContextLookup . ContainsKey ( modelInfo . Type . Assembly ) ||
164- IsSameAssembly ( contextGenerationSpec . Type , modelInfo . Type ) ;
170+ contextGenerationSpec . Type . IsSameAssembly ( modelInfo . Type ) ;
165171 }
166172
167173 private static void EmitModelInfo (
@@ -193,7 +199,6 @@ private static void EmitModelInfo(
193199 EmitReadOnlyMemoryModelInfo ( indent , builder , modelInfo , context , referenceContextLookup , identifierLookup ) ;
194200 break ;
195201 default :
196- //give warning and skip
197202 break ;
198203 }
199204 }
@@ -206,7 +211,7 @@ private static void EmitReadOnlyMemoryModelInfo(
206211 Dictionary < string , TypeRef > referenceContextLookup ,
207212 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
208213 {
209- var elementType = modelInfo . Type . GenericArguments [ 0 ] ;
214+ var elementType = modelInfo . Type . ItemType ! ;
210215 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
211216 builder . AppendLine ( indent , "{" ) ;
212217 indent ++ ;
@@ -262,7 +267,7 @@ private static void EmitMultiDimensionalArrayModelInfo(
262267 Dictionary < string , TypeRef > referenceContextLookup ,
263268 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
264269 {
265- var elementType = modelInfo . Type . GenericArguments [ 0 ] ;
270+ var elementType = modelInfo . Type . ItemType ! ;
266271 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
267272 builder . AppendLine ( indent , "{" ) ;
268273 indent ++ ;
@@ -329,7 +334,7 @@ private static void EmitArrayModelInfo(
329334 Dictionary < string , TypeRef > referenceContextLookup ,
330335 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
331336 {
332- var elementType = modelInfo . Type . GenericArguments [ 0 ] ;
337+ var elementType = modelInfo . Type . ItemType ! ;
333338 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
334339 builder . AppendLine ( indent , "{" ) ;
335340 indent ++ ;
@@ -366,7 +371,7 @@ private static void EmitDictionaryModelInfo(
366371 Dictionary < string , TypeRef > referenceContextLookup ,
367372 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
368373 {
369- var elementType = modelInfo . Type . GenericArguments [ 1 ] ;
374+ var elementType = modelInfo . Type . ItemType ! ;
370375 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
371376 builder . AppendLine ( indent , "{" ) ;
372377 indent ++ ;
@@ -389,21 +394,6 @@ private static void EmitDictionaryModelInfo(
389394 builder . AppendLine ( indent , "}" ) ;
390395 }
391396
392- private static bool IsSameAssembly ( TypeRef context , TypeRef elementType )
393- {
394- if ( context . Assembly . Equals ( elementType . Assembly , StringComparison . Ordinal ) )
395- return true ;
396-
397- //If we made the context implicitly its assembly will be a simple name
398- //TestAssembly
399- //vs
400- //TestAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
401- if ( ! context . Assembly . AsSpan ( ) . Contains ( ", Version" . AsSpan ( ) , StringComparison . Ordinal ) )
402- return elementType . Assembly . StartsWith ( context . Assembly , StringComparison . Ordinal ) ;
403-
404- return false ;
405- }
406-
407397 private static void EmitEnumerableModelInfo (
408398 int indent ,
409399 StringBuilder builder ,
@@ -412,7 +402,7 @@ private static void EmitEnumerableModelInfo(
412402 Dictionary < string , TypeRef > referenceContextLookup ,
413403 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
414404 {
415- var elementType = modelInfo . Type . GenericArguments [ 0 ] ;
405+ var elementType = modelInfo . Type . ItemType ! ;
416406 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
417407 builder . AppendLine ( indent , "{" ) ;
418408 indent ++ ;
@@ -442,7 +432,7 @@ private static void EmitPersistableModelInfo(
442432 TypeRef context ,
443433 Dictionary < TypeRef , ( string TypeCase , string CamelCase ) > identifierLookup )
444434 {
445- if ( IsSameAssembly ( context , modelInfo . Type ) )
435+ if ( context . IsSameAssembly ( modelInfo . Type ) )
446436 {
447437 builder . AppendLine ( indent , $ "internal class { identifierLookup [ modelInfo . Type ] . TypeCase } Builder : ModelReaderWriterTypeBuilder") ;
448438 builder . AppendLine ( indent , "{" ) ;
@@ -483,17 +473,17 @@ private HashSet<string> GetNameSpaces(ModelReaderWriterContextGenerationSpec con
483473 return namespaces ;
484474 }
485475
486- private void AddNamespaces ( HashSet < string > namespaces , TypeRef referencedContext , HashSet < TypeRef > visited )
476+ private void AddNamespaces ( HashSet < string > namespaces , TypeRef type , HashSet < TypeRef > visited )
487477 {
488- if ( ! visited . Add ( referencedContext ) )
478+ if ( ! visited . Add ( type ) )
489479 {
490480 return ;
491481 }
492482
493- namespaces . Add ( referencedContext . Namespace ) ;
494- foreach ( var genericArgument in referencedContext . GenericArguments )
483+ namespaces . Add ( type . Namespace ) ;
484+ if ( type . ItemType is not null )
495485 {
496- AddNamespaces ( namespaces , genericArgument , visited ) ;
486+ AddNamespaces ( namespaces , type . ItemType , visited ) ;
497487 }
498488 }
499489
0 commit comments