From b66bc4e5ed1d2144f23ce31052ed6f671eb21c84 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Mon, 29 Jul 2024 20:25:22 -0700 Subject: [PATCH] Title: Added support for 'Do' attribute in XML Formatter x) Introduced the 'Do' attribute to the XML formatter. x) The 'Do' attribute is now fetched and checked before filtering fields and properties. x) This change allows more granular control over serialization, as items marked with the 'Do' attribute will not be filtered out. x) Adjusted formatting in some areas for consistency. --- ser/XmlFormatter2.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ser/XmlFormatter2.cs b/ser/XmlFormatter2.cs index b9f13a1..d960bc6 100644 --- a/ser/XmlFormatter2.cs +++ b/ser/XmlFormatter2.cs @@ -561,6 +561,7 @@ namespace lib string name = childFi.Name; var dontAtt = childFi.GetCustomAttributes(); + var doAtt = childFi.GetCustomAttributes(); string propName = ""; @@ -573,6 +574,8 @@ namespace lib var propInfo = narrowType.GetProperty( propName ); dontAtt = propInfo.GetCustomAttributes(); + + doAtt = propInfo.GetCustomAttributes(); } if( dontAtt.Any() ) @@ -586,7 +589,7 @@ namespace lib name = refl.TypeToIdentifier( name ); // @@@ TODO This doesnt yet handle propNames! - if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) + if( !doAtt.Any() && FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) continue; var useFieldName = true; @@ -641,9 +644,12 @@ namespace lib continue; } + var doAtt = childPi.GetCustomAttributes(); + + name = refl.TypeToIdentifier( name ); - if( FilterField( filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name ) ) + if( !doAtt.Any() && FilterField( filterProps, doImpls, whitelistProps, childPi as PropertyInfo, name ) ) continue; XmlElement childElem = getNamedChild( allChildren, name ); @@ -1338,6 +1344,7 @@ namespace lib { string name = childFi.Name; var dontAtt = childFi.GetCustomAttributes(); + var doAtt = childFi.GetCustomAttributes(); string propName = ""; if( name.StartsWith( "<" ) && name.EndsWith( "BackingField" ) ) @@ -1349,6 +1356,7 @@ namespace lib var propInfo = narrowType.GetProperty( propName ); dontAtt = propInfo.GetCustomAttributes(); + doAtt = propInfo.GetCustomAttributes(); } @@ -1363,7 +1371,7 @@ namespace lib if( name == "Fn" ) continue; } - if( FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) + if( !doAtt.Any() && FilterField( filterFields, doImpls, whitelistFields, childFi as MemberInfo, name ) ) continue; object[] objs = childFi.GetCustomAttributes( typeof( NonSerializedAttribute ), true ); @@ -1429,15 +1437,15 @@ namespace lib var custWLProps = mi?.GetCustomAttribute( true ); filterFields = custWLFields != null; - filterProps = custWLProps != null; + filterProps = custWLProps != null; var typesTodo = type.GetCustomAttribute( true )?.Types ?? TypesDefault; - doImpls = typesTodo.HasFlag( Types.Implied ); + doImpls = typesTodo.HasFlag( Types.Implied ); doFields = filterFields || typesTodo.HasFlag( Types.Fields ); - doProps = filterProps || typesTodo.HasFlag( Types.Props ); + doProps = filterProps || typesTodo.HasFlag( Types.Props ); whitelistFields = new( custWLFields?.Values ?? new string[0] ); - whitelistProps = new( custWLProps?.Values ?? new string[0] ); + whitelistProps = new( custWLProps?.Values ?? new string[0] ); } private void SerializeArray( XmlWriter writer, MemberInfo mi, Type mType, object root, int depth )