Skip to content

Commit

Permalink
Improved support for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Aug 14, 2018
1 parent 20919f5 commit 75c07fd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ public IEnumerable<ClassModel> AllBaseClasses
}
}

public IEnumerable<TypeModel> AllBaseTypes
{
get
{
var baseType = BaseClass;
while (baseType != null)
{
yield return baseType;
baseType = (baseType as ClassModel)?.BaseClass;
}
}
}

public override CodeTypeDeclaration Generate()
{
var classDeclaration = base.Generate();
Expand Down Expand Up @@ -399,13 +412,15 @@ public List<ClassModel> GetAllDerivedTypes()

public override CodeExpression GetDefaultValueFor(string defaultString)
{
if (BaseClass is SimpleModel)
var rootClass = AllBaseTypes.LastOrDefault();

if (rootClass is SimpleModel)
{
string reference, val;

using (var writer = new System.IO.StringWriter())
{
CSharpProvider.GenerateCodeFromExpression(BaseClass.GetDefaultValueFor(defaultString), writer, new CodeGeneratorOptions());
CSharpProvider.GenerateCodeFromExpression(rootClass.GetDefaultValueFor(defaultString), writer, new CodeGeneratorOptions());
val = writer.ToString();
}

Expand Down

0 comments on commit 75c07fd

Please sign in to comment.