using HotelPms.Share.Data; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace HotelPms.Share.Util { /// /// ソースコード自動作成 /// public static class ScriptCreator { /// /// クラス名でCreateInstance /// /// hoObj /// string public static object ClassForName(string codeBase, string hsClsName) { System.Reflection.Assembly woAssem = null; Type woType = null; try { woAssem = System.Reflection.Assembly.Load(codeBase); woType = woAssem.GetType(hsClsName); return Activator.CreateInstance(woType); } catch(Exception ex) { return null; } } /// /// クラス名でCreateInstance /// /// hoObj /// string public static object ClassForName(string hsClsName) { return ClassForName(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, hsClsName); } /// /// クラス ⇒ テキストボックス /// /// /// /// public static string GetClassToTextBox(string codeBase, string className) { StringBuilder sb = new StringBuilder(); object item = ClassForName(codeBase, className); Type type = item.GetType(); PropertyInfo[] infos = type.GetProperties(); foreach (PropertyInfo field in infos) { string name = field.Name; if (field.PropertyType.FullName.Contains("System.Collections.Generic.List")) { continue; } if (field.PropertyType.FullName.Contains("System.Data.DataSet")) { continue; } if (field.PropertyType.FullName.Contains("System.Object")) { continue; } if ("System.Boolean".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = item.{name} ? \"1\" : \"0\";"); } else if ("System.Decimal".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = item.{name}.ToString();"); } else if ("System.Int32".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = item.{name}.ToString();"); } else if ("System.String".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = item.{name};"); } else if ("System.DateTime".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = CConvert.ToDateString(item.{name});"); } else if ("customTypes.Date".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = CConvert.ToDateString(item.{name}.ToDateTime());"); } else if ("customTypes.DecimalValue".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = item.{name}.ToText();"); } else if ("Google.Protobuf.WellKnownTypes.Timestamp".Equals(field.PropertyType.FullName)) { sb.AppendLine($"txt{name}.Text = CConvert.ToDateString(item.{name}.ToDateTime());"); } else { continue; } } return sb.ToString(); } /// /// テキストボックス⇒クラス /// /// Projectの名称:例えば Forms、Lib /// /// public static string GetTextBoxToClass(string codeBase, string className) { StringBuilder sb = new StringBuilder(); object item = ClassForName(codeBase, className); Type type = item.GetType(); PropertyInfo[] infos = type.GetProperties(); foreach(PropertyInfo field in infos) { string name = field.Name; if (field.PropertyType.FullName.Contains("System.Collections.Generic.List")) { continue; } if (field.PropertyType.FullName.Contains("System.Data.DataSet")) { continue; } if (field.PropertyType.FullName.Contains("System.Object")) { continue; } if ("System.Boolean".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToBool(txt{name}.Text);"); } else if ("System.Decimal".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToDecimal(txt{name}.Text);"); } else if ("System.Int32".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToInt(txt{name}.Text);"); } else if ("System.String".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = txt{name}.Text;"); } else if ("System.DateTime".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToDateTime(txt{name}.Text);"); } else if ("customTypes.Date".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = new Date(CConvert.ToDateInt(txt{name}.Text));"); } else if ("customTypes.DecimalValue".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToDecimal(txt{name}.Text);"); } else if ("Google.Protobuf.WellKnownTypes.Timestamp".Equals(field.PropertyType.FullName)) { sb.AppendLine($"item.{name} = CConvert.ToTimestamp(CConvert.ToDateTime(txt{name}.Text));"); } else { continue; } } return sb.ToString(); } } }