From 2dae885fff6ea2f86ec1edd4976863b7b4728c18 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Mon, 30 Oct 2023 15:37:45 +0100 Subject: [PATCH] Support ShouldSerialize pattern with collections (fixes #436) --- XmlSchemaClassGenerator/TypeModel.cs | 56 ++++++++++++++++++---------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 7702d82d..16e353b2 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -882,17 +882,6 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi } else if (isEnumerable && !IsRequired) { - var specifiedProperty = new CodeMemberProperty - { - Type = TypeRef(), - Name = Name + Specified, - HasSet = false, - HasGet = true, - }; - specifiedProperty.CustomAttributes.Add(ignoreAttribute); - if (Configuration.EntityFramework) { specifiedProperty.CustomAttributes.Add(notMappedAttribute); } - specifiedProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final; - var listReference = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), Name); var collectionType = Configuration.CollectionImplementationType ?? Configuration.CollectionType; var countProperty = collectionType == typeof(Array) ? nameof(Array.Length) : nameof(List.Count); @@ -904,17 +893,46 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi notZeroExpression = new CodeBinaryOperatorExpression(notNullExpression, CodeBinaryOperatorType.BooleanAnd, notZeroExpression); } var returnStatement = new CodeMethodReturnStatement(notZeroExpression); - specifiedProperty.GetStatements.Add(returnStatement); - var specifiedDocs = new DocumentationModel[] { - new() { Language = English, Text = $"Gets a value indicating whether the {Name} collection is empty." }, - new() { Language = German, Text = $"Ruft einen Wert ab, der angibt, ob die {Name}-Collection leer ist." } - }; - specifiedProperty.Comments.AddRange(GetComments(specifiedDocs).ToArray()); + if (Configuration.UseShouldSerializePattern) + { + var shouldSerializeMethod = new CodeMemberMethod + { + Attributes = MemberAttributes.Public, + Name = "ShouldSerialize" + Name, + ReturnType = new CodeTypeReference(typeof(bool)), + Statements = { returnStatement } + }; + + Configuration.MemberVisitor(shouldSerializeMethod, this); + + typeDeclaration.Members.Add(shouldSerializeMethod); + } + else + { + var specifiedProperty = new CodeMemberProperty + { + Type = TypeRef(), + Name = Name + Specified, + HasSet = false, + HasGet = true, + }; + specifiedProperty.CustomAttributes.Add(ignoreAttribute); + if (Configuration.EntityFramework) { specifiedProperty.CustomAttributes.Add(notMappedAttribute); } + specifiedProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final; - Configuration.MemberVisitor(specifiedProperty, this); + specifiedProperty.GetStatements.Add(returnStatement); - typeDeclaration.Members.Add(specifiedProperty); + var specifiedDocs = new DocumentationModel[] { + new() { Language = English, Text = $"Gets a value indicating whether the {Name} collection is empty." }, + new() { Language = German, Text = $"Ruft einen Wert ab, der angibt, ob die {Name}-Collection leer ist." } + }; + specifiedProperty.Comments.AddRange(GetComments(specifiedDocs).ToArray()); + + Configuration.MemberVisitor(specifiedProperty, this); + + typeDeclaration.Members.Add(specifiedProperty); + } } if (IsNullableReferenceType && Configuration.EnableNullableReferenceAttributes)