@@ -28,6 +28,8 @@ private sealed partial class Emitter
28
28
private const string DefaultOptionsStaticVarName = "s_defaultOptions" ;
29
29
private const string DefaultContextBackingStaticVarName = "s_defaultContext" ;
30
30
internal const string GetConverterFromFactoryMethodName = "GetConverterFromFactory" ;
31
+ private const string GetTypeInfoCoreMethodName = "GetTypeInfoCore" ;
32
+ private const string GetTypeInfoMethodName = "GetTypeInfo" ;
31
33
private const string InfoVarName = "info" ;
32
34
private const string PropertyInfoVarName = "propertyInfo" ;
33
35
internal const string JsonContextVarName = "jsonContext" ;
@@ -37,6 +39,7 @@ private sealed partial class Emitter
37
39
private const string JsonTypeInfoReturnValueLocalVariableName = "jsonTypeInfo" ;
38
40
private const string PropInitMethodNameSuffix = "PropInit" ;
39
41
private const string RuntimeCustomConverterFetchingMethodName = "GetRuntimeProvidedCustomConverter" ;
42
+ private const string TypeLocalVariableName = "type" ;
40
43
private const string SerializeHandlerPropName = "SerializeHandler" ;
41
44
private const string ValueVarName = "value" ;
42
45
private const string WriterVarName = "writer" ;
@@ -1227,9 +1230,9 @@ private static string GetFetchLogicForGetCustomConverter_TypesWithFactories()
1227
1230
{
1228
1231
return @$"
1229
1232
1230
- private static { JsonConverterTypeRef } { GetConverterFromFactoryMethodName } ( { JsonSerializerOptionsTypeRef } { OptionsLocalVariableName } , { TypeTypeRef } type , { JsonConverterFactoryTypeRef } factory)
1233
+ private static { JsonConverterTypeRef } { GetConverterFromFactoryMethodName } ( { JsonSerializerOptionsTypeRef } { OptionsLocalVariableName } , { TypeTypeRef } { TypeLocalVariableName } , { JsonConverterFactoryTypeRef } factory)
1231
1234
{ {
1232
- { JsonConverterTypeRef } ? converter = factory . CreateConverter ( type , { OptionsLocalVariableName } ) ;
1235
+ { JsonConverterTypeRef } ? converter = factory . CreateConverter ( { TypeLocalVariableName } , { OptionsLocalVariableName } ) ;
1233
1236
if ( converter == null || converter is { JsonConverterFactoryTypeRef } )
1234
1237
{ {
1235
1238
throw new { InvalidOperationExceptionTypeRef } ( string . Format ( "" { ExceptionMessages . InvalidJsonConverterFactoryOutput } "" , factory . GetType ( ) ) ) ;
@@ -1245,56 +1248,33 @@ private string GetGetTypeInfoImplementation()
1245
1248
1246
1249
sb . Append (
1247
1250
@$"/// <inheritdoc/>
1248
- public override { JsonTypeInfoTypeRef } GetTypeInfo ( { TypeTypeRef } type)
1249
- { { ") ;
1250
-
1251
- HashSet < TypeGenerationSpec > types = new ( _currentContext . TypesWithMetadataGenerated ) ;
1251
+ public override { JsonTypeInfoTypeRef } { GetTypeInfoMethodName } ( { TypeTypeRef } { TypeLocalVariableName } )
1252
+ => { GetTypeInfoCoreMethodName } ( { TypeLocalVariableName } , { OptionsInstanceVariableName } ) ;
1252
1253
1253
- // TODO (https://github.com/dotnet/runtime/issues/52218): Make this Dictionary-lookup-based if root-serializable type count > 64.
1254
- foreach ( TypeGenerationSpec metadata in types)
1255
- {
1256
- if ( metadata . ClassType != ClassType . TypeUnsupportedBySourceGen )
1257
- {
1258
- sb. Append ( $@ "
1259
- if (type == typeof({ metadata . TypeRef } ))
1260
- {{
1261
- return this.{ metadata . TypeInfoPropertyName } ;
1262
- }}
1254
+ { JsonTypeInfoTypeRef } ? { JsonTypeInfoResolverTypeRef } . { GetTypeInfoMethodName } ( { TypeTypeRef } { TypeLocalVariableName } , { JsonSerializerOptionsTypeRef } { OptionsLocalVariableName } )
1255
+ => { GetTypeInfoCoreMethodName } ( { TypeLocalVariableName } , { OptionsLocalVariableName } ) ;
1263
1256
") ;
1264
- }
1265
- }
1266
-
1267
- sb. AppendLine ( @"
1268
- return null!;
1269
- }" ) ;
1270
1257
1271
- // Explicit IJsonTypeInfoResolver implementation
1272
1258
sb . AppendLine ( ) ;
1273
- sb. Append ( @$ "{ JsonTypeInfoTypeRef } ? { JsonTypeInfoResolverTypeRef } .GetTypeInfo({ TypeTypeRef } type, { JsonSerializerOptionsTypeRef } { OptionsLocalVariableName } )
1274
- {{
1275
- if ({ OptionsInstanceVariableName } == { OptionsLocalVariableName } )
1276
- {{
1277
- return this.GetTypeInfo(type);
1278
- }}
1279
- else
1280
- {{" ) ;
1259
+ sb . Append ( @$"private {JsonTypeInfoTypeRef}? {GetTypeInfoCoreMethodName}({TypeTypeRef} {TypeLocalVariableName}, {JsonSerializerOptionsTypeRef} {OptionsLocalVariableName})
1260
+ { { ");
1261
+
1281
1262
// TODO (https://github.com/dotnet/runtime/issues/52218): Make this Dictionary-lookup-based if root-serializable type count > 64.
1282
- foreach ( TypeGenerationSpec metadata in types )
1263
+ foreach ( TypeGenerationSpec metadata in _currentContext . TypesWithMetadataGenerated )
1283
1264
{
1284
1265
if ( metadata . ClassType != ClassType . TypeUnsupportedBySourceGen )
1285
1266
{
1286
1267
sb. Append ( $@ "
1287
- if (type == typeof({ metadata . TypeRef } ))
1288
- {{
1289
- return { metadata . CreateTypeInfoMethodName } ({ OptionsLocalVariableName } );
1290
- }}
1268
+ if ({ TypeLocalVariableName } == typeof({ metadata . TypeRef } ))
1269
+ {{
1270
+ return { metadata . CreateTypeInfoMethodName } ({ OptionsLocalVariableName } );
1271
+ }}
1291
1272
" ) ;
1292
1273
}
1293
1274
}
1294
1275
1295
1276
sb. Append ( $@ "
1296
- return null;
1297
- }}
1277
+ return null;
1298
1278
}}
1299
1279
" ) ;
1300
1280
0 commit comments