Skip to content

Commit

Permalink
Support ShouldSerialize pattern with collections (fixes #436)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Oct 30, 2023
1 parent a29c835 commit 2dae885
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,17 +882,6 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
}
else if (isEnumerable && !IsRequired)
{
var specifiedProperty = new CodeMemberProperty
{
Type = TypeRef<bool>(),
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<int>.Count);
Expand All @@ -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<bool>(),
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)
Expand Down

0 comments on commit 2dae885

Please sign in to comment.