Skip to content

Commit 1b8d119

Browse files
author
Michael Ganss
committed
Merge branch 'master' of github.com:mganss/XmlSchemaClassGenerator
2 parents 56d8428 + 4f812a9 commit 1b8d119

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static void Main(string[] args)
3535
string textValuePropertyName = "Value";
3636
var generateDebuggerStepThroughAttribute = true;
3737
var disableComments = false;
38+
var doNotUseUnderscoreInPrivateMemberNames = false;
3839

3940
var options = new OptionSet {
4041
{ "h|help", "show this message and exit", v => showHelp = v != null },
@@ -77,6 +78,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
7778
{ "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v },
7879
{ "dst|debuggerStepThrough", "generate DebuggerStepThroughAttribute (default is enabled)", v => generateDebuggerStepThroughAttribute = v != null },
7980
{ "dc|disableComments", "do not include comments from xsd", v => disableComments = v != null },
81+
{ "nu|noUnderscore", "do not generate underscore in private member name (default is false)", v => doNotUseUnderscoreInPrivateMemberNames = v != null },
8082
};
8183

8284
var files = options.Parse(args);
@@ -130,7 +132,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
130132
generator.GenerateDebuggerStepThroughAttribute = false;
131133
generator.DataAnnotationMode = DataAnnotationMode.None;
132134
}
133-
135+
generator.DoNotUseUnderscoreInPrivateMemberNames = doNotUseUnderscoreInPrivateMemberNames;
134136
if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); }
135137

136138
generator.Generate(files);

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static string ToCamelCase(this string s)
2727
return char.ToLowerInvariant(s[0]) + s.Substring(1);
2828
}
2929

30-
public static string ToBackingField(this string propertyName)
30+
public static string ToBackingField(this string propertyName, bool doNotUseUnderscoreInPrivateMemberNames)
3131
{
32-
return string.Concat("_", propertyName.ToCamelCase());
32+
return doNotUseUnderscoreInPrivateMemberNames ? propertyName.ToCamelCase() : string.Concat("_", propertyName.ToCamelCase());
3333
}
3434

3535
private static bool? IsDataTypeAttributeAllowed(XmlTypeCode typeCode, GeneratorConfiguration configuration)
@@ -171,17 +171,21 @@ public static string GetUniqueTypeName(this NamespaceModel model, string name)
171171

172172
public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel propertyModel)
173173
{
174-
var propBackingFieldName = propertyModel.Name.ToBackingField();
175174
var classModel = typeModel as ClassModel;
175+
var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.DoNotUseUnderscoreInPrivateMemberNames == true);
176+
177+
if (CSharpKeywords.Contains(propBackingFieldName.ToLower()))
178+
propBackingFieldName = "@" + propBackingFieldName;
179+
176180
if (classModel == null)
177181
{
178182
return propBackingFieldName;
179183
}
180-
184+
181185
var i = 0;
182186
foreach (var prop in classModel.Properties)
183187
{
184-
if (!classModel.EnableDataBinding && !(prop.Type is SimpleModel))
188+
if (!classModel.Configuration.EnableDataBinding && !(prop.Type is SimpleModel))
185189
{
186190
continue;
187191
}
@@ -192,7 +196,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
192196
break;
193197
}
194198

195-
var backingFieldName = prop.Name.ToBackingField();
199+
var backingFieldName = prop.Name.ToBackingField(classModel.Configuration.DoNotUseUnderscoreInPrivateMemberNames);
196200
if (backingFieldName == propBackingFieldName)
197201
{
198202
i += 1;
@@ -207,11 +211,35 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
207211
return string.Format("{0}{1}", propBackingFieldName, i);
208212
}
209213

210-
static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n",RegexOptions.Compiled);
214+
static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n", RegexOptions.Compiled);
211215

212216
internal static string NormalizeNewlines(string text)
213217
{
214218
return NormalizeNewlinesRegex.Replace(text, "$1\r\n");
215219
}
220+
221+
static readonly List<string> CSharpKeywords = new List<string>
222+
{
223+
"abstract", "as", "base", "bool",
224+
"break", "byte", "case", "catch",
225+
"char", "checked", "class", "const",
226+
"continue", "decimal", "default", "delegate",
227+
"do", "double", "else", "enum",
228+
"event", " explicit", "extern", "false",
229+
"finally", "fixed", "float", "for",
230+
"foreach", "goto", "if", "implicit",
231+
"in", "int", "interface", "internal",
232+
"is", "lock", "long", "namespace",
233+
"new", "null", "object", "operator",
234+
"out", "override", "params", "private",
235+
"protected", "public", "readonly", "ref",
236+
"return", "sbyte", "sealed", "short",
237+
"sizeof", "stackalloc", "static", "string",
238+
"struct", "switch", "this", "throw",
239+
"true", "try", "typeof", "uint",
240+
"ulong", "unchecked", "unsafe", "ushort",
241+
"using", "using static", "virtual", "void",
242+
"volatile", "while"
243+
};
216244
}
217245
}

XmlSchemaClassGenerator/Generator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ public bool DisableComments
172172
get { return _configuration.DisableComments; }
173173
set { _configuration.DisableComments = value; }
174174
}
175+
176+
public bool DoNotUseUnderscoreInPrivateMemberNames
177+
{
178+
get { return _configuration.DoNotUseUnderscoreInPrivateMemberNames; }
179+
set { _configuration.DoNotUseUnderscoreInPrivateMemberNames = value; }
180+
}
175181

176182
private readonly XmlSchemaSet Set = new XmlSchemaSet();
177183
private Dictionary<XmlQualifiedName, XmlSchemaAttributeGroup> AttributeGroups;
@@ -188,8 +194,8 @@ public void Generate(IEnumerable<string> files)
188194
}));
189195

190196
foreach (var s in schemas)
191-
{
192-
Set.Add(s);
197+
{
198+
Set.Add(s.TargetNamespace, s.SourceUri);
193199
}
194200

195201
Set.Compile();
@@ -413,8 +419,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif
413419
IsAbstract = complexType.IsAbstract,
414420
IsAnonymous = complexType.QualifiedName.Name == "",
415421
IsMixed = complexType.IsMixed,
416-
IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty,
417-
EnableDataBinding = EnableDataBinding,
422+
IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty
418423
};
419424

420425
classModel.Documentation.AddRange(docs);

XmlSchemaClassGenerator/GeneratorConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,7 @@ public void WriteLog(string message)
158158
public NamingProvider NamingProvider { get; set; }
159159

160160
public bool DisableComments { get; set; }
161+
public bool DoNotUseUnderscoreInPrivateMemberNames { get; set; }
162+
161163
}
162164
}

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static CodeNamespace Generate(string namespaceName, IEnumerable<Namespace
4545
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));
4646

4747
var typeModels = parts.SelectMany(x => x.Types.Values).ToList();
48-
if (typeModels.OfType<ClassModel>().Any(x => x.EnableDataBinding))
48+
if (typeModels.OfType<ClassModel>().Any(x => x.Configuration.EnableDataBinding))
4949
{
5050
codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));
5151
codeNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
@@ -210,8 +210,6 @@ public class ClassModel : TypeModel
210210
public List<PropertyModel> Properties { get; set; }
211211
public List<InterfaceModel> Interfaces { get; set; }
212212
public List<ClassModel> DerivedTypes { get; set; }
213-
public bool EnableDataBinding { get; set; }
214-
215213
public ClassModel(GeneratorConfiguration configuration)
216214
: base(configuration)
217215
{
@@ -243,7 +241,7 @@ public override CodeTypeDeclaration Generate()
243241
classDeclaration.IsClass = true;
244242
classDeclaration.IsPartial = true;
245243

246-
if (EnableDataBinding)
244+
if (Configuration.EnableDataBinding)
247245
{
248246
classDeclaration.Members.Add(new CodeMemberEvent()
249247
{
@@ -290,9 +288,9 @@ public override CodeTypeDeclaration Generate()
290288
Attributes = MemberAttributes.Public,
291289
};
292290

293-
if (EnableDataBinding)
291+
if (Configuration.EnableDataBinding)
294292
{
295-
var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField())
293+
var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField(Configuration.DoNotUseUnderscoreInPrivateMemberNames))
296294
{
297295
Attributes = MemberAttributes.Private
298296
};
@@ -325,7 +323,7 @@ public override CodeTypeDeclaration Generate()
325323
}
326324
}
327325

328-
if (EnableDataBinding)
326+
if (Configuration.EnableDataBinding)
329327
{
330328
classDeclaration.BaseTypes.Add(new CodeTypeReference(typeof(INotifyPropertyChanged), Configuration.CodeTypeReferenceOptions));
331329
}
@@ -353,7 +351,7 @@ public override CodeTypeDeclaration Generate()
353351
}
354352

355353
foreach (var property in Properties)
356-
property.AddMembersTo(classDeclaration, EnableDataBinding);
354+
property.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
357355

358356
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
359357
{
@@ -692,6 +690,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
692690
member = new CodeMemberField(typeReference, propertyName);
693691

694692
var isPrivateSetter = IsCollection || isArray;
693+
695694
if (requiresBackingField)
696695
{
697696
member.Name += GetAccessors(member.Name, backingField.Name,

0 commit comments

Comments
 (0)