From 620d65b8c0432416374f9dbcc158ca0efe158fba Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 11 Jun 2024 10:01:47 +0100 Subject: [PATCH] Use Type.GetProperty/GetField instead of GetMember. --- .../System.Text.Json/gen/JsonSourceGenerator.Emitter.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index 1ed487e6bbd73c..1def217cc33c54 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -70,6 +70,7 @@ private sealed partial class Emitter private const string JsonPropertyInfoValuesTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues"; private const string JsonTypeInfoTypeRef = "global::System.Text.Json.Serialization.Metadata.JsonTypeInfo"; private const string JsonTypeInfoResolverTypeRef = "global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver"; + private const string EmptyTypeArray = "global::System.Array.Empty()"; /// /// Contains an index from TypeRef to TypeGenerationSpec for the current ContextGenerationSpec. @@ -519,7 +520,7 @@ private SourceText GenerateForObject(ContextGenerationSpec contextSpec, TypeGene or ObjectConstructionStrategy.ParameterizedConstructor) { string argTypes = typeMetadata.CtorParamGenSpecs.Count == 0 - ? "global::System.Array.Empty()" + ? EmptyTypeArray : $$"""new[] {{{string.Join(", ", typeMetadata.CtorParamGenSpecs.Select(p => $"typeof({p.ParameterType.FullyQualifiedName})"))}}}"""; constructorInfoFactoryFunc = $"static () => typeof({typeMetadata.TypeRef.FullyQualifiedName}).GetConstructor({InstanceMemberBindingFlagsVariableName}, binder: null, {argTypes}, modifiers: null)"; @@ -649,6 +650,10 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe : $"({JsonConverterTypeRef}<{propertyTypeFQN}>){ExpandConverterMethodName}(typeof({propertyTypeFQN}), new {converterFQN}(), {OptionsLocalVariableName})"; } + string attributeProviderFactoryExpr = property.IsProperty + ? $"typeof({property.DeclaringType.FullyQualifiedName}).GetProperty({FormatStringLiteral(property.MemberName)}, {InstanceMemberBindingFlagsVariableName}, null, typeof({property.PropertyType.FullyQualifiedName}), {EmptyTypeArray}, null)" + : $"typeof({property.DeclaringType.FullyQualifiedName}).GetField({FormatStringLiteral(property.MemberName)}, {InstanceMemberBindingFlagsVariableName})"; + writer.WriteLine($$""" var {{InfoVarName}}{{i}} = new {{JsonPropertyInfoValuesTypeRef}}<{{propertyTypeFQN}}> { @@ -665,7 +670,7 @@ private void GeneratePropMetadataInitFunc(SourceWriter writer, string propInitMe NumberHandling = {{FormatNumberHandling(property.NumberHandling)}}, PropertyName = {{FormatStringLiteral(property.MemberName)}}, JsonPropertyName = {{FormatStringLiteral(property.JsonPropertyName)}}, - AttributeProviderFactory = static () => typeof({{property.DeclaringType.FullyQualifiedName}}).GetMember({{FormatStringLiteral(property.MemberName)}}, {{InstanceMemberBindingFlagsVariableName}}) is { Length: > 0 } results ? results[0] : null, + AttributeProviderFactory = static () => {{attributeProviderFactoryExpr}}, }; properties[{{i}}] = {{JsonMetadataServicesTypeRef}}.CreatePropertyInfo<{{propertyTypeFQN}}>({{OptionsLocalVariableName}}, {{InfoVarName}}{{i}});