Skip to content

Commit 01b0a30

Browse files
Use Type.GetProperty/GetField instead of GetMember. (#103275)
1 parent 91f1481 commit 01b0a30

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private sealed partial class Emitter
7070
private const string JsonPropertyInfoValuesTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues";
7171
private const string JsonTypeInfoTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonTypeInfo";
7272
private const string JsonTypeInfoResolverTypeRef = "global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver";
73+
private const string EmptyTypeArray = "global::System.Array.Empty<global::System.Type>()";
7374

7475
/// <summary>
7576
/// Contains an index from TypeRef to TypeGenerationSpec for the current ContextGenerationSpec.
@@ -519,7 +520,7 @@ private SourceText GenerateForObject(ContextGenerationSpec contextSpec, TypeGene
519520
or ObjectConstructionStrategy.ParameterizedConstructor)
520521
{
521522
string argTypes = typeMetadata.CtorParamGenSpecs.Count == 0
522-
? "global::System.Array.Empty<global::System.Type>()"
523+
? EmptyTypeArray
523524
: $$"""new[] {{{string.Join(", ", typeMetadata.CtorParamGenSpecs.Select(p => $"typeof({p.ParameterType.FullyQualifiedName})"))}}}""";
524525

525526
constructorInfoFactoryFunc = $"static () => typeof({typeMetadata.TypeRef.FullyQualifiedName}).GetConstructor({InstanceMemberBindingFlagsVariableName}, binder: null, {argTypes}, modifiers: null)";
@@ -649,6 +650,10 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe
649650
: $"({JsonConverterTypeRef}<{propertyTypeFQN}>){ExpandConverterMethodName}(typeof({propertyTypeFQN}), new {converterFQN}(), {OptionsLocalVariableName})";
650651
}
651652

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+
652657
writer.WriteLine($$"""
653658
var {{InfoVarName}}{{i}} = new {{JsonPropertyInfoValuesTypeRef}}<{{propertyTypeFQN}}>
654659
{
@@ -665,7 +670,7 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe
665670
NumberHandling = {{FormatNumberHandling(property.NumberHandling)}},
666671
PropertyName = {{FormatStringLiteral(property.MemberName)}},
667672
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}},
669674
};
670675
671676
properties[{{i}}] = {{JsonMetadataServicesTypeRef}}.CreatePropertyInfo<{{propertyTypeFQN}}>({{OptionsLocalVariableName}}, {{InfoVarName}}{{i}});

0 commit comments

Comments
 (0)