@@ -70,6 +70,7 @@ private sealed partial class Emitter
70
70
private const string JsonPropertyInfoValuesTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues" ;
71
71
private const string JsonTypeInfoTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonTypeInfo" ;
72
72
private const string JsonTypeInfoResolverTypeRef = "global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver" ;
73
+ private const string EmptyTypeArray = "global::System.Array.Empty<global::System.Type>()" ;
73
74
74
75
/// <summary>
75
76
/// Contains an index from TypeRef to TypeGenerationSpec for the current ContextGenerationSpec.
@@ -519,7 +520,7 @@ private SourceText GenerateForObject(ContextGenerationSpec contextSpec, TypeGene
519
520
or ObjectConstructionStrategy . ParameterizedConstructor )
520
521
{
521
522
string argTypes = typeMetadata . CtorParamGenSpecs . Count == 0
522
- ? "global::System.Array.Empty<global::System.Type>()"
523
+ ? EmptyTypeArray
523
524
: $$ """ new[] {{{string.Join(", ", typeMetadata.CtorParamGenSpecs.Select(p => $"typeof({p.ParameterType.FullyQualifiedName})"))}}}""" ;
524
525
525
526
constructorInfoFactoryFunc = $ "static () => typeof({ typeMetadata . TypeRef . FullyQualifiedName } ).GetConstructor({ InstanceMemberBindingFlagsVariableName } , binder: null, { argTypes } , modifiers: null)";
@@ -649,6 +650,10 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe
649
650
: $ "({ JsonConverterTypeRef } <{ propertyTypeFQN } >){ ExpandConverterMethodName } (typeof({ propertyTypeFQN } ), new { converterFQN } (), { OptionsLocalVariableName } )";
650
651
}
651
652
653
+ string attributeProviderFactoryExpr = property . IsProperty
654
+ ? $ "typeof({ property . DeclaringType . FullyQualifiedName } ).GetProperty({ FormatStringLiteral ( property . MemberName ) } , { InstanceMemberBindingFlagsVariableName } , null, typeof({ property . PropertyType . FullyQualifiedName } ), { EmptyTypeArray } , null)"
655
+ : $ "typeof({ property . DeclaringType . FullyQualifiedName } ).GetField({ FormatStringLiteral ( property . MemberName ) } , { InstanceMemberBindingFlagsVariableName } )";
656
+
652
657
writer . WriteLine ( $$ """
653
658
var {{ InfoVarName }} {{ i }} = new {{ JsonPropertyInfoValuesTypeRef }} <{{ propertyTypeFQN }} >
654
659
{
@@ -665,7 +670,7 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe
665
670
NumberHandling = {{ FormatNumberHandling ( property . NumberHandling ) }} ,
666
671
PropertyName = {{ FormatStringLiteral ( property . MemberName ) }} ,
667
672
JsonPropertyName = {{ FormatStringLiteral ( property . JsonPropertyName ) }} ,
668
- AttributeProviderFactory = static () => typeof( {{ property . DeclaringType . FullyQualifiedName }} ).GetMember( {{ FormatStringLiteral ( property . MemberName ) }} , {{ InstanceMemberBindingFlagsVariableName }} ) is { Length: > 0 } results ? results[0] : null ,
673
+ AttributeProviderFactory = static () => {{ attributeProviderFactoryExpr }} ,
669
674
};
670
675
671
676
properties[{{ i }} ] = {{ JsonMetadataServicesTypeRef }} .CreatePropertyInfo<{{ propertyTypeFQN }} >({{ OptionsLocalVariableName }} , {{ InfoVarName }} {{ i }} );
0 commit comments