From 01b0a30b48c78c98e54901a356a47a1019f24c74 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 18 Jun 2024 20:42:09 +0100 Subject: [PATCH] Use Type.GetProperty/GetField instead of GetMember. (#103275) --- .../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 10f5ac7c1ef3b..11bf6ae9f3cf6 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}});