Skip to content

Commit

Permalink
Use Type.GetProperty/GetField instead of GetMember.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Jun 11, 2024
1 parent c897ce6 commit 620d65b
Showing 1 changed file with 7 additions and 2 deletions.
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

0 comments on commit 620d65b

Please sign in to comment.