Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Type.GetProperty/GetField instead of GetMember. #103275

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<global::System.Type>()";

/// <summary>
/// Contains an index from TypeRef to TypeGenerationSpec for the current ContextGenerationSpec.
Expand Down Expand Up @@ -519,7 +520,7 @@ private SourceText GenerateForObject(ContextGenerationSpec contextSpec, TypeGene
or ObjectConstructionStrategy.ParameterizedConstructor)
{
string argTypes = typeMetadata.CtorParamGenSpecs.Count == 0
? "global::System.Array.Empty<global::System.Type>()"
? 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)";
Expand Down Expand Up @@ -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}}>
{
Expand All @@ -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}});
Expand Down
Loading