Skip to content

Commit

Permalink
Add arg to override the name of the text value property
Browse files Browse the repository at this point in the history
  • Loading branch information
jahmai-ca committed Apr 8, 2017
1 parent fc8d0cb commit c6e7cc6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
7 changes: 5 additions & 2 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static void Main(string[] args)
var collectionType = typeof(Collection<>);
Type collectionImplementationType = null;
var codeTypeReferenceOptions = default(CodeTypeReferenceOptions);
string textValuePropertyName = "Value";

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -70,7 +71,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "a|pascal", "use Pascal case for class and property names (default is enabled)", v => pascal = v != null },
{ "ct|collectionType=", "collection type to use (default is " + typeof(Collection<>).FullName + ")", v => collectionType = v == null ? typeof(Collection<>) : Type.GetType(v, true) },
{ "cit|collectionImplementationType=", "the default collection type implementation to use (default is null)", v => collectionImplementationType = v == null ? null : Type.GetType(v, true) },
{ "ctro|codeTypeReferenceOptions=", "the default CodeTypeReferenceOptions Flags to use (default is unset; can be: {GlobalReference, GenericTypeParameter})", v => codeTypeReferenceOptions = v == null ? default(CodeTypeReferenceOptions) : (CodeTypeReferenceOptions)Enum.Parse(typeof(CodeTypeReferenceOptions), v, false) }
{ "ctro|codeTypeReferenceOptions=", "the default CodeTypeReferenceOptions Flags to use (default is unset; can be: {GlobalReference, GenericTypeParameter})", v => codeTypeReferenceOptions = v == null ? default(CodeTypeReferenceOptions) : (CodeTypeReferenceOptions)Enum.Parse(typeof(CodeTypeReferenceOptions), v, false) },
{ "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v }
};

var files = options.Parse(args);
Expand Down Expand Up @@ -110,7 +112,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
NamingScheme = pascal ? NamingScheme.PascalCase : NamingScheme.Direct,
CollectionType = collectionType,
CollectionImplementationType = collectionImplementationType,
CodeTypeReferenceOptions = codeTypeReferenceOptions
CodeTypeReferenceOptions = codeTypeReferenceOptions,
TextValuePropertyName = textValuePropertyName
};

if (pclCompatible)
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ public CodeTypeReferenceOptions CodeTypeReferenceOptions
set { _configuration.CodeTypeReferenceOptions = value; }
}

public string TextValuePropertyName
{
get { return _configuration.TextValuePropertyName; }
set { _configuration.TextValuePropertyName = value; }
}

/// <summary>
/// Optional delegate that is called for each generated type member
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public GeneratorConfiguration()
/// </summary>
public CodeTypeReferenceOptions CodeTypeReferenceOptions { get; set; }

/// <summary>
/// The name of the property that will contain the text value of an XML element
/// </summary>
public string TextValuePropertyName { get; set; } = "Value";

/// <summary>
/// Provides a fast and safe way to write to the Log
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ public override CodeTypeDeclaration Generate()
{
classDeclaration.BaseTypes.Add(BaseClass.GetReferenceFor(Namespace, false));
}
else
else if (!string.IsNullOrEmpty(Configuration.TextValuePropertyName))
{
var typeReference = BaseClass.GetReferenceFor(Namespace, false);

var member = new CodeMemberField(typeReference, "Value")
var member = new CodeMemberField(typeReference, Configuration.TextValuePropertyName)
{
Attributes = MemberAttributes.Public,
};
Expand Down

0 comments on commit c6e7cc6

Please sign in to comment.