namespace Framework.Cli.Generate { using Framework.DataAccessLayer; using System; using System.Collections.Generic; using System.Reflection; using System.Text; /// /// Util functions for code generation. /// internal class UtilGenerate { /// /// Filter out special characters. Allow only characters and numbers. /// private static string NameCSharp(string name) { StringBuilder result = new StringBuilder(); bool isFirst = true; foreach (char item in name) { if (item >= '0' && item <= '9') { if (isFirst) { result.Append("_"); // If first char is a number } result.Append(item); } char itemToUpper = char.ToUpper(item); if (itemToUpper >= 'A' && itemToUpper <= 'Z') { result.Append(item); } isFirst = false; } return result.ToString(); } /// /// Returns CSharp code compliant name. /// public static string NameCSharp(string name, List nameExceptList) { var nameExceptListCopy = new List(nameExceptList); // Do not modify list passed as parameter. for (int i = 0; i < nameExceptListCopy.Count; i++) { nameExceptListCopy[i] = NameCSharp(nameExceptListCopy[i]).ToUpper(); } // name = NameCSharp(name); string result = name; int count = 1; while (nameExceptListCopy.Contains(result.ToUpper())) { count += 1; result = name + count; } nameExceptList.Add(name); return result; } /// /// Returns CSharp code. /// /// For example: "Int32" /// Returns "int" private static string TypeToCSharpType(Type type) { if (type == typeof(Int32)) { return "int"; } if (type == typeof(String)) { return "string"; } if (type == typeof(Boolean)) { return "bool"; } if (type == typeof(Double)) { return "double"; } if (type == typeof(Byte[])) { return "byte[]"; } return type.Name; } /// /// SqlType to CSharp code. /// public static string SqlTypeToCSharpType(int sqlType, bool isNullable) { Type type = UtilDalType.SqlTypeToType(sqlType); string result = TypeToCSharpType(type); if (type.IsValueType) { if (isNullable) { result += "?"; } } return result; } } }