Skip to content

Commit

Permalink
Read the DataMemberAttribute on write operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Metalaka committed Apr 21, 2023
1 parent 3afa6f4 commit c29e645
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/AvroConvert/AvroObjectServices/Write/Resolvers/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,20 @@ private static void HandleExpando(WriteStep[] writers, IWriter encoder, ExpandoO
var calHash = Expression.Assign(nameHash,
Expression.Call(memberName, typeof(object).GetMethod("GetHashCode")));
var cases = new List<SwitchCase>();

foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy))
{
// Bind the physical property to a getter
var getterName = propertyInfo.Name;
var dataMemberAttribute = propertyInfo.GetCustomAttribute<DataMemberAttribute>();
if (dataMemberAttribute?.Name is not null)
{
// If there is a DataMemberAttribute, we want to bind the getter with that value
getterName = dataMemberAttribute.Name;
}

var property = Expression.Property(Expression.Convert(instance, type), propertyInfo.Name);
var propertyHash = Expression.Constant(propertyInfo.Name.GetHashCode(), typeof(int));
var propertyHash = Expression.Constant(getterName.GetHashCode(), typeof(int));

cases.Add(Expression.SwitchCase(Expression.Convert(property, typeof(object)), propertyHash));
}
Expand All @@ -121,4 +131,4 @@ private static void HandleExpando(WriteStep[] writers, IWriter encoder, ExpandoO

}
}
}
}

0 comments on commit c29e645

Please sign in to comment.