/// <summary> /// Tests whether the word conflicts with reserved or language keywords, and if so, attempts to return a /// valid word that does not conflict. Usually the returned word is only slightly modified to differentiate /// the identifier from the keyword; for example, the word might be preceded by the underscore ("_") character. /// </summary> /// <param name="word">The word.</param> /// <returns></returns> private static string FixReservedWord(string word) { // turns keywords into usable words. // i.e. class -> _class Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); return(codeProvider.CreateValidIdentifier(word)); }
/// <summary> /// Extract the schema definition from an SqlCommand. /// </summary> /// <param name="command">SqlCommand</param> /// <param name="result">list of columns</param> private void GetSchemaFromReader(SqlCommand command, List <Column> result) { using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly)) { DataTable schema = reader.GetSchemaTable(); if (schema != null) { Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); foreach (DataRow row in schema.Rows) { Column col = new Column() { Name = codeProvider.CreateValidIdentifier((string)row["ColumnName"]), SysType = GetSysType((string)row["DataTypeName"], (bool)row["AllowDBNull"]), DbType = GetDbType((string)row["DataTypeName"]).ToString(), IsNullable = (bool)row["AllowDBNull"] }; result.Add(col); } // add unique names for columns without a name // ensure that column names are unique and add a sequence number if neccessary string nameTemplate = "Column{0}"; int columnNr = 1; foreach (Column col in result) { if (String.IsNullOrEmpty(col.Name)) { string newName; do { newName = String.Format(nameTemplate, columnNr++); } while (result.Any(x => x.Name == newName)); col.Name = newName; } else { string colName = col.Name; int seq = 1; while (result.Any(x => x != col && x.Name == col.Name)) { col.Name = colName + (seq++).ToString(); } } } } } }
public string GenerateValidToken(string token) { using (var code = new Microsoft.CSharp.CSharpCodeProvider( )) { var validToken = ""; var salt = 0; do { if (Tokens.Contains(token)) { validToken = string.Format("{0}{1}", token, salt); } else { validToken = code.CreateValidIdentifier(token); } salt++; } while(Tokens.Contains(validToken)); Tokens.Add(validToken); return(validToken); } }
public void GenerateCode(CodeDomGenerator.Language language) { switch (language) { case CodeDomGenerator.Language.CSharp: using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions(); using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString)) { tw.Write("foreach ("); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts); tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName)); provider.GenerateCodeFromExpression(IterExpression, tw, opts); tw.WriteLine(")"); tw.WriteLine("{"); tw.Indent++; if (Statements != null) { foreach (CodeStatement statement in Statements) { provider.GenerateCodeFromStatement(statement, tw, opts); } } tw.Indent--; tw.WriteLine("}"); Value = tw.InnerWriter.ToString(); } } break; case CodeDomGenerator.Language.VB: using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider()) { System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions(); using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString)) { tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName)); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts); tw.Write(" in "); provider.GenerateCodeFromExpression(IterExpression, tw, opts); tw.WriteLine(); tw.Indent++; if (Statements != null) { foreach (CodeStatement statement in Statements) { provider.GenerateCodeFromStatement(statement, tw, opts); } } tw.Indent--; tw.WriteLine("Next"); Value = tw.InnerWriter.ToString(); } } break; default: throw new NotImplementedException(language.ToString()); } }
public void GenerateCode(CodeDomGenerator.Language language) { switch (language) { case CodeDomGenerator.Language.CSharp: using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions(); using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString)) { tw.Write("foreach ("); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts); tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName)); provider.GenerateCodeFromExpression(IterExpression, tw, opts); tw.WriteLine(")"); tw.WriteLine("{"); tw.Indent++; if (Statements != null) foreach (CodeStatement statement in Statements) { provider.GenerateCodeFromStatement(statement, tw, opts); } tw.Indent--; tw.WriteLine("}"); Value = tw.InnerWriter.ToString(); } } break; case CodeDomGenerator.Language.VB: using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider()) { System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions(); using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString)) { tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName)); provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts); tw.Write(" in "); provider.GenerateCodeFromExpression(IterExpression, tw, opts); tw.WriteLine(); tw.Indent++; if (Statements != null) foreach (CodeStatement statement in Statements) { provider.GenerateCodeFromStatement(statement, tw, opts); } tw.Indent--; tw.WriteLine("Next"); Value = tw.InnerWriter.ToString(); } } break; default: throw new NotImplementedException(language.ToString()); } }
/// <summary> /// Tests whether the word conflicts with reserved or language keywords, and if so, attempts to return a /// valid word that does not conflict. Usually the returned word is only slightly modified to differentiate /// the identifier from the keyword; for example, the word might be preceded by the underscore ("_") character. /// </summary> /// <param name="word">The word.</param> /// <returns></returns> private static string FixReservedWord(string word) { // turns keywords into usable words. // i.e. class -> _class Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); return codeProvider.CreateValidIdentifier(word); }