diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index 0e95e8ee..bdabdb7b 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -35,6 +35,7 @@ static void Main(string[] args) string textValuePropertyName = "Value"; var generateDebuggerStepThroughAttribute = true; var disableComments = false; + var doNotUseUnderscoreInPrivateMemberNames = false; var options = new OptionSet { { "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 { "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v }, { "dst|debuggerStepThrough", "generate DebuggerStepThroughAttribute (default is enabled)", v => generateDebuggerStepThroughAttribute = v != null }, { "dc|disableComments", "do not include comments from xsd", v => disableComments = v != null }, + { "nu|noUnderscore", "do not generate underscore in private member name (default is false)", v => doNotUseUnderscoreInPrivateMemberNames = v != null }, }; 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 generator.GenerateDebuggerStepThroughAttribute = false; generator.DataAnnotationMode = DataAnnotationMode.None; } - + generator.DoNotUseUnderscoreInPrivateMemberNames = doNotUseUnderscoreInPrivateMemberNames; if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); } generator.Generate(files); diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index 9a43bf69..e83a4012 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -27,9 +27,9 @@ public static string ToCamelCase(this string s) return char.ToLowerInvariant(s[0]) + s.Substring(1); } - public static string ToBackingField(this string propertyName) + public static string ToBackingField(this string propertyName, bool doNotUseUnderscoreInPrivateMemberNames) { - return string.Concat("_", propertyName.ToCamelCase()); + return doNotUseUnderscoreInPrivateMemberNames ? propertyName.ToCamelCase() : string.Concat("_", propertyName.ToCamelCase()); } private static bool? IsDataTypeAttributeAllowed(XmlTypeCode typeCode, GeneratorConfiguration configuration) @@ -171,17 +171,21 @@ public static string GetUniqueTypeName(this NamespaceModel model, string name) public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel propertyModel) { - var propBackingFieldName = propertyModel.Name.ToBackingField(); var classModel = typeModel as ClassModel; + var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.DoNotUseUnderscoreInPrivateMemberNames == true); + + if (CSharpKeywords.Contains(propBackingFieldName.ToLower())) + propBackingFieldName = "@" + propBackingFieldName; + if (classModel == null) { return propBackingFieldName; } - + var i = 0; foreach (var prop in classModel.Properties) { - if (!classModel.EnableDataBinding && !(prop.Type is SimpleModel)) + if (!classModel.Configuration.EnableDataBinding && !(prop.Type is SimpleModel)) { continue; } @@ -192,7 +196,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel break; } - var backingFieldName = prop.Name.ToBackingField(); + var backingFieldName = prop.Name.ToBackingField(classModel.Configuration.DoNotUseUnderscoreInPrivateMemberNames); if (backingFieldName == propBackingFieldName) { i += 1; @@ -207,11 +211,35 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel return string.Format("{0}{1}", propBackingFieldName, i); } - static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n",RegexOptions.Compiled); + static readonly Regex NormalizeNewlinesRegex = new Regex(@"(^|[^\r])\n", RegexOptions.Compiled); internal static string NormalizeNewlines(string text) { return NormalizeNewlinesRegex.Replace(text, "$1\r\n"); } + + static readonly List CSharpKeywords = new List + { + "abstract", "as", "base", "bool", + "break", "byte", "case", "catch", + "char", "checked", "class", "const", + "continue", "decimal", "default", "delegate", + "do", "double", "else", "enum", + "event", " explicit", "extern", "false", + "finally", "fixed", "float", "for", + "foreach", "goto", "if", "implicit", + "in", "int", "interface", "internal", + "is", "lock", "long", "namespace", + "new", "null", "object", "operator", + "out", "override", "params", "private", + "protected", "public", "readonly", "ref", + "return", "sbyte", "sealed", "short", + "sizeof", "stackalloc", "static", "string", + "struct", "switch", "this", "throw", + "true", "try", "typeof", "uint", + "ulong", "unchecked", "unsafe", "ushort", + "using", "using static", "virtual", "void", + "volatile", "while" + }; } } \ No newline at end of file diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index 8b9b6757..d65a4460 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -172,6 +172,12 @@ public bool DisableComments get { return _configuration.DisableComments; } set { _configuration.DisableComments = value; } } + + public bool DoNotUseUnderscoreInPrivateMemberNames + { + get { return _configuration.DoNotUseUnderscoreInPrivateMemberNames; } + set { _configuration.DoNotUseUnderscoreInPrivateMemberNames = value; } + } private readonly XmlSchemaSet Set = new XmlSchemaSet(); private Dictionary AttributeGroups; @@ -413,8 +419,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAnnotated type, XmlQualif IsAbstract = complexType.IsAbstract, IsAnonymous = complexType.QualifiedName.Name == "", IsMixed = complexType.IsMixed, - IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty, - EnableDataBinding = EnableDataBinding, + IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty }; classModel.Documentation.AddRange(docs); diff --git a/XmlSchemaClassGenerator/GeneratorConfiguration.cs b/XmlSchemaClassGenerator/GeneratorConfiguration.cs index c16d02dc..97793809 100644 --- a/XmlSchemaClassGenerator/GeneratorConfiguration.cs +++ b/XmlSchemaClassGenerator/GeneratorConfiguration.cs @@ -158,5 +158,7 @@ public void WriteLog(string message) public NamingProvider NamingProvider { get; set; } public bool DisableComments { get; set; } + public bool DoNotUseUnderscoreInPrivateMemberNames { get; set; } + } } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 3d1b3325..72b3ef19 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -45,7 +45,7 @@ public static CodeNamespace Generate(string namespaceName, IEnumerable x.Types.Values).ToList(); - if (typeModels.OfType().Any(x => x.EnableDataBinding)) + if (typeModels.OfType().Any(x => x.Configuration.EnableDataBinding)) { codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq")); codeNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel")); @@ -210,8 +210,6 @@ public class ClassModel : TypeModel public List Properties { get; set; } public List Interfaces { get; set; } public List DerivedTypes { get; set; } - public bool EnableDataBinding { get; set; } - public ClassModel(GeneratorConfiguration configuration) : base(configuration) { @@ -243,7 +241,7 @@ public override CodeTypeDeclaration Generate() classDeclaration.IsClass = true; classDeclaration.IsPartial = true; - if (EnableDataBinding) + if (Configuration.EnableDataBinding) { classDeclaration.Members.Add(new CodeMemberEvent() { @@ -290,9 +288,9 @@ public override CodeTypeDeclaration Generate() Attributes = MemberAttributes.Public, }; - if (EnableDataBinding) + if (Configuration.EnableDataBinding) { - var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField()) + var backingFieldMember = new CodeMemberField(typeReference, member.Name.ToBackingField(Configuration.DoNotUseUnderscoreInPrivateMemberNames)) { Attributes = MemberAttributes.Private }; @@ -325,7 +323,7 @@ public override CodeTypeDeclaration Generate() } } - if (EnableDataBinding) + if (Configuration.EnableDataBinding) { classDeclaration.BaseTypes.Add(new CodeTypeReference(typeof(INotifyPropertyChanged), Configuration.CodeTypeReferenceOptions)); } @@ -353,7 +351,7 @@ public override CodeTypeDeclaration Generate() } foreach (var property in Properties) - property.AddMembersTo(classDeclaration, EnableDataBinding); + property.AddMembersTo(classDeclaration, Configuration.EnableDataBinding); if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed)))) { @@ -692,6 +690,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi member = new CodeMemberField(typeReference, propertyName); var isPrivateSetter = IsCollection || isArray; + if (requiresBackingField) { member.Name += GetAccessors(member.Name, backingField.Name,