Skip to content

Commit

Permalink
Always generate backing field for collections (needed by XmlSerialize…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jun 11, 2015
1 parent 49cd164 commit fe3d1e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 0 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private Assembly Compile(string name, string pattern)
IntegerDataType = typeof(int),
DataAnnotationMode = DataAnnotationMode.Partial,
GenerateDesignerCategoryAttribute = false,
EnableDataBinding = true
};

var files = Glob.Glob.ExpandNames(pattern);
Expand Down
21 changes: 18 additions & 3 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,20 @@ internal static string GetAccessors(string memberName, string backingFieldName,
}}", backingFieldName, memberName, (privateSetter ? "private " : string.Empty));
}
}
return string.Format(@"

if (privateSetter)
{
return string.Format(@"
{{
get
{{
return this.{0};
}}
}}", backingFieldName);
}
else
{
return string.Format(@"
{{
get
{{
Expand All @@ -426,6 +439,7 @@ internal static string GetAccessors(string memberName, string backingFieldName,
this.{0} = value;
}}
}}", backingFieldName, (privateSetter ? "private " : string.Empty));
}
}

// ReSharper disable once FunctionComplexityOverflow
Expand All @@ -445,7 +459,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
var typeReference = propertyType.GetReferenceFor(OwningType.Namespace, IsCollection || isArray);
var simpleType = propertyType as SimpleModel;

var requiresBackingField = withDataBinding || DefaultValue != null;
var requiresBackingField = withDataBinding || DefaultValue != null || IsCollection || isArray;
var backingField = new CodeMemberField(typeReference, OwningType.GetUniqueFieldName(this))
{
Attributes = MemberAttributes.Private
Expand Down Expand Up @@ -635,7 +649,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
constructor.Comments.AddRange(DocumentationModel.GetComments(constructorDocs).ToArray());
typeDeclaration.Members.Add(constructor);
}
var listReference = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), Name);
var listReference = requiresBackingField ? (CodeExpression)new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), backingField.Name) :
new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), Name);
var initTypeReference = propertyType.GetReferenceFor(OwningType.Namespace, true, true);
var initExpression = new CodeObjectCreateExpression(initTypeReference);
constructor.Statements.Add(new CodeAssignStatement(listReference, initExpression));
Expand Down

0 comments on commit fe3d1e9

Please sign in to comment.