Skip to content

Commit 53aed3f

Browse files
committed
Combine IsCollection and IsArray into IsEnumerable, and use for nullable attribute.
1 parent 8204bee commit 53aed3f

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -601,26 +601,28 @@ internal static string GetAccessors(string memberName, string backingFieldName,
601601
&& !TypeClassModel.Properties[0].IsAttribute && !TypeClassModel.Properties[0].IsAny
602602
&& TypeClassModel.Properties[0].IsCollection;
603603

604+
private bool IsEnumerable => IsCollection || IsArray;
605+
604606
private TypeModel PropertyType => !IsArray ? Type : TypeClassModel.Properties[0].Type;
605607

606608
private bool IsNullableValueType => DefaultValue == null
607-
&& IsNullable && !(IsCollection || IsArray) && !IsList
609+
&& IsNullable && !IsEnumerable && !IsList
608610
&& ((PropertyType is EnumModel) || (PropertyType is SimpleModel model && model.ValueType.IsValueType));
609611

610612
private bool IsNullableReferenceType => DefaultValue == null
611-
&& IsNullable && (IsCollection || IsArray || IsList || PropertyType is ClassModel || (PropertyType is SimpleModel model && !model.ValueType.IsValueType));
613+
&& IsNullable && (IsEnumerable || IsList || PropertyType is ClassModel || (PropertyType is SimpleModel model && !model.ValueType.IsValueType));
612614

613615
private bool IsNillableValueType => IsNillable
614-
&& !(IsCollection || IsArray)
616+
&& !IsEnumerable
615617
&& ((PropertyType is EnumModel) || (PropertyType is SimpleModel model && model.ValueType.IsValueType));
616618

617619
private bool IsList => Type.XmlSchemaType?.Datatype?.Variety == XmlSchemaDatatypeVariety.List;
618620

619621
private bool IsPrivateSetter => Configuration.CollectionSettersMode == CollectionSettersMode.Private
620-
&& (IsCollection || IsArray || (IsList && IsAttribute));
622+
&& (IsEnumerable || (IsList && IsAttribute));
621623

622624
private CodeTypeReference TypeReference => PropertyType.GetReferenceFor(OwningType.Namespace,
623-
collection: IsCollection || IsArray || (IsList && IsAttribute),
625+
collection: IsEnumerable || (IsList && IsAttribute),
624626
attribute: IsAttribute);
625627

626628
private void AddDocs(CodeTypeMember member)
@@ -694,6 +696,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
694696

695697
var typeClassModel = TypeClassModel;
696698
var isArray = IsArray;
699+
var isEnumerable = IsEnumerable;
697700
var propertyType = PropertyType;
698701
var isNullableValueType = IsNullableValueType;
699702
var isNullableReferenceType = IsNullableReferenceType;
@@ -704,7 +707,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
704707
CodeAttributeDeclaration notMappedAttribute = new(CodeUtilities.CreateTypeReference(Attributes.NotMapped, Configuration));
705708

706709
CodeMemberField backingField = null;
707-
if (withDataBinding || DefaultValue != null || IsCollection || isArray)
710+
if (withDataBinding || DefaultValue != null || isEnumerable)
708711
{
709712
backingField = IsNillableValueType
710713
? new CodeMemberField(NullableTypeRef(typeReference), OwningType.GetUniqueFieldName(this))
@@ -713,7 +716,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
713716
typeDeclaration.Members.Add(backingField);
714717
}
715718

716-
if (DefaultValue == null || ((IsCollection || isArray || (IsList && IsAttribute)) && IsNullable))
719+
if (DefaultValue == null || ((isEnumerable || (IsList && IsAttribute)) && IsNullable))
717720
{
718721
if (isNullableValueType && Configuration.GenerateNullables && !(Configuration.UseShouldSerializePattern && !IsAttribute))
719722
member.Name += Value;
@@ -739,16 +742,9 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
739742
member.Type = typeReference;
740743
}
741744

742-
if (backingField != null)
743-
{
744-
var propertyValueTypeCode = IsCollection || isArray ? PropertyValueTypeCode.Array : propertyType.GetPropertyValueTypeCode();
745-
member.Name += GetAccessors(member.Name, backingField.Name, propertyValueTypeCode, isPrivateSetter, withDataBinding);
746-
}
747-
else
748-
{
749-
var privateSetter = isPrivateSetter ? "private " : string.Empty;
750-
member.Name += $" {{ get; {privateSetter}set; }}"; // hack to generate automatic property
751-
}
745+
member.Name += backingField != null
746+
? GetAccessors(member.Name, backingField.Name, isEnumerable ? PropertyValueTypeCode.Array : propertyType.GetPropertyValueTypeCode(), isPrivateSetter, withDataBinding)
747+
: $" {{ get; {(isPrivateSetter ? "private " : string.Empty)}set; }}"; // hack to generate automatic property
752748
}
753749
else
754750
{
@@ -878,7 +874,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
878874
Configuration.MemberVisitor(nullableMember, this);
879875
}
880876
}
881-
else if ((IsCollection || isArray || (IsList && IsAttribute)) && IsNullable)
877+
else if ((isEnumerable || (IsList && IsAttribute)) && IsNullable)
882878
{
883879
var specifiedProperty = new CodeMemberProperty
884880
{
@@ -915,7 +911,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
915911
typeDeclaration.Members.Add(specifiedProperty);
916912
}
917913

918-
if (!IsCollection && isNullableReferenceType && Configuration.EnableNullableReferenceAttributes)
914+
if (!IsEnumerable && isNullableReferenceType && Configuration.EnableNullableReferenceAttributes)
919915
{
920916
member.CustomAttributes.Add(new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(Attributes.AllowNull, Configuration)));
921917
member.CustomAttributes.Add(new CodeAttributeDeclaration(CodeUtilities.CreateTypeReference(Attributes.MaybeNull, Configuration)));
@@ -925,7 +921,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
925921
member.CustomAttributes.AddRange(attributes);
926922

927923
// initialize List<>
928-
if ((IsCollection || isArray || (IsList && IsAttribute)) && Configuration.CollectionSettersMode != CollectionSettersMode.PublicWithoutConstructorInitialization)
924+
if ((isEnumerable || (IsList && IsAttribute)) && Configuration.CollectionSettersMode != CollectionSettersMode.PublicWithoutConstructorInitialization)
929925
{
930926
var constructor = typeDeclaration.Members.OfType<CodeConstructor>().FirstOrDefault();
931927

0 commit comments

Comments
 (0)