Skip to content

Commit

Permalink
Fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Dec 5, 2017
1 parent 20cabe2 commit 790a656
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public static IEnumerable<CodeCommentStatement> GetComments(IEnumerable<Document

public abstract class TypeModel
{
protected static readonly CodeDomProvider CSharpProvider = CodeDomProvider.CreateProvider("CSharp");

public NamespaceModel Namespace { get; set; }
public XmlQualifiedName RootElementName { get; set; }
public string Name { get; set; }
Expand Down Expand Up @@ -401,6 +403,31 @@ public List<ClassModel> GetAllDerivedTypes()

return allDerivedTypes;
}

public override CodeExpression GetDefaultValueFor(string defaultString)
{
if (BaseClass is SimpleModel)
{
string reference, val;

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

using (var writer = new System.IO.StringWriter())
{
CSharpProvider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(GetReferenceFor(null, false)), writer, new CodeGeneratorOptions());
reference = writer.ToString();
}

var dv = new CodeSnippetExpression($"new { reference } {{ { Configuration.TextValuePropertyName } = { val } }};");
return dv;
}

return base.GetDefaultValueFor(defaultString);
}
}

public class PropertyModel
Expand Down Expand Up @@ -609,7 +636,7 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration)
{
var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue);

if (!(defaultValueExpression is CodeObjectCreateExpression))
if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression))
{
var defaultValueAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(DefaultValueAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(defaultValueExpression));
Expand Down Expand Up @@ -692,7 +719,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi

if (IsNullable)
{
if (!(defaultValueExpression is CodeObjectCreateExpression))
if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression))
{
var defaultValueAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(DefaultValueAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(defaultValueExpression));
Expand Down Expand Up @@ -1064,8 +1091,6 @@ public override CodeExpression GetDefaultValueFor(string defaultString)

public class SimpleModel : TypeModel
{
private static readonly CodeDomProvider CSharpProvider = CodeDomProvider.CreateProvider("CSharp");

public Type ValueType { get; set; }
public List<RestrictionModel> Restrictions { get; private set; }
public bool UseDataTypeAttribute { get; set; }
Expand Down

0 comments on commit 790a656

Please sign in to comment.