@@ -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}
0 commit comments