ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
{
    /// <summary>
    /// ソースコード自動作成
    /// </summary>
    public static class ScriptCreator
    {
        /// <summary>
        ///   クラス名でCreateInstance
        /// </summary>
        /// <param name="hoObj">hoObj</param>
        /// <returns>string</returns>
        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;
            }
        }
 
        /// <summary>
        ///   クラス名でCreateInstance
        /// </summary>
        /// <param name="hoObj">hoObj</param>
        /// <returns>string</returns>
        public static object ClassForName(string hsClsName)
        {
            return ClassForName(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, hsClsName);
        }
 
        /// <summary>
        /// クラス ⇒ テキストボックス
        /// </summary>
        /// <param name="codeBase"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        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();
        }
 
        /// <summary>
        /// テキストボックス⇒クラス
        /// </summary>
        /// <param name="codeBase">Projectの名称:例えば Forms、Lib</param>
        /// <param name="className"></param>
        /// <returns></returns>
        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();
        }
    }
}