本博客所有文章分類的總目錄:http://www.cnblogs.com/asxinyu/p/4288836.html
Newlife XCode組件相關文章目錄:http://www.cnblogs.com/asxinyu/p/4329747.html
先說明一下,本文使用的Xcode不是Mac的Xcode,而且Newlife團隊開發的一個.NET開發組件。其歷史也有將近10年,因此大家不要誤會。
本文對應的論壇地址為:擁有自己的代碼生成器-Xcoder代碼生成器分析,
新生命開發團隊的相關信息,QQ群:1600800
博客:http://nnhy.cnblogs.com
論壇:http://www.53wb.com
博客:http://nnhy.cnblogs.com
論壇:http://www.53wb.com
前面幾篇博客斷斷續續介紹了一些Newlife.Xcode的使用,特別是在獲取數據庫架構信息方面,是最有用的。當然Newlife系列組件的強大並不止前面介紹的哪些,更重要的是它能給你帶來很多想象和發揮的空間,比如我今天的主題——擁有自己的代碼生成器。因為有了模板引擎,Xcoder可以讓你省去很多重復的事情,因為Xcode可以很容易的獲取到數據庫架構的所有信息,所以就可以用來做你想做的事情,很多事情也因此變得簡單。
越來越發現,寫程序原來也是件很藝術的事情,雖然我沒有寫出那么多藝術的代碼。
這篇博客主要是簡單分析Xcoder代碼生成器的原理及模板引擎的快速使用方法,了解了這些,擁有自己的代碼生成器就很容易。首先來簡單看看Xcoder的源碼,大概看了下,可能不是那么徹底啊。
一、Xcoder中的代碼生成功能,主要是調用Engine(代碼生成器類)來完成核心工作,只不過在界面上獲取和設置一些東西,比如命名空間,模板等等。代碼生成器工作的時候也是傳入相對應的表名稱。
1.有一個常量 TemplatePath = "Template";這個是模板的默認路徑,一般是當前應用程序下的Template文件夾
2.靜態屬性FileTemplates,會搜索模板目錄下的所有模板
3.Engine初始化時是傳入XConfig類,也就是配置信息啊。里面包含了當前代碼生成器的一些設置信息,比如界面上的,數據庫連接名,命名空間,模板名稱,輸出目錄等。有了這些信息,獲取數據庫架構信息就很容易了。就不列舉了,
4.最核心的方法就是Render了,由於Xcoder代碼生成器主要是針對數據庫架構來生成代碼,所以一般情況下,要做自己的代碼生成器,用Engine就足夠了。可是有些情況下,生成一些批量的代碼不一定是從數據庫來獲取元數據的信息,比如我要搞的這個東西,封裝WMI中的類,上百個呢(打算做20-30個常用的吧),多不多,但太累,並且是重復的工作,沒有技術含量。所以就想辦法用代碼生成器了,當然需要自己改造下,那接下來就主要看看Render方法里面是怎么調用模板來生成代碼的,這個搞明白了,自己也就可以調用XTemplate來生成代碼了。
5.Render方法里面主要就是通過配置信息,來加載模板文件及內容,當然也要搜索模板目錄下的所有模板文件,然后放到Dictionary中,這是一個鍵值對,分別為模板名稱和模板內容。然后用這個Dictionary來獲得一個Template對象,然后編譯模板,關鍵代碼:
這篇博客主要是簡單分析Xcoder代碼生成器的原理及模板引擎的快速使用方法,了解了這些,擁有自己的代碼生成器就很容易。首先來簡單看看Xcoder的源碼,大概看了下,可能不是那么徹底啊。
一、Xcoder中的代碼生成功能,主要是調用Engine(代碼生成器類)來完成核心工作,只不過在界面上獲取和設置一些東西,比如命名空間,模板等等。代碼生成器工作的時候也是傳入相對應的表名稱。
1.有一個常量 TemplatePath = "Template";這個是模板的默認路徑,一般是當前應用程序下的Template文件夾
2.靜態屬性FileTemplates,會搜索模板目錄下的所有模板
3.Engine初始化時是傳入XConfig類,也就是配置信息啊。里面包含了當前代碼生成器的一些設置信息,比如界面上的,數據庫連接名,命名空間,模板名稱,輸出目錄等。有了這些信息,獲取數據庫架構信息就很容易了。就不列舉了,
4.最核心的方法就是Render了,由於Xcoder代碼生成器主要是針對數據庫架構來生成代碼,所以一般情況下,要做自己的代碼生成器,用Engine就足夠了。可是有些情況下,生成一些批量的代碼不一定是從數據庫來獲取元數據的信息,比如我要搞的這個東西,封裝WMI中的類,上百個呢(打算做20-30個常用的吧),多不多,但太累,並且是重復的工作,沒有技術含量。所以就想辦法用代碼生成器了,當然需要自己改造下,那接下來就主要看看Render方法里面是怎么調用模板來生成代碼的,這個搞明白了,自己也就可以調用XTemplate來生成代碼了。
5.Render方法里面主要就是通過配置信息,來加載模板文件及內容,當然也要搜索模板目錄下的所有模板文件,然后放到Dictionary中,這是一個鍵值對,分別為模板名稱和模板內容。然后用這個Dictionary來獲得一個Template對象,然后編譯模板,關鍵代碼:
1 Template tt = Template.Create(templates);
2 if (tempName.StartsWith( " * "))
3 tempName = tempName.Substring( 1);
4 tt.AssemblyName = tempName;
5 tt.Compile(); // 編譯模板
2 if (tempName.StartsWith( " * "))
3 tempName = tempName.Substring( 1);
4 tt.AssemblyName = tempName;
5 tt.Compile(); // 編譯模板
二、上面是Xcoder的大概分析,其實Xcoder包括的東西很多,要做代碼生成器,肯定要參考這個,里面自動更新,數據庫架構信息獲取、編輯都有。下面來說說怎么用XTemplate來快速調用模板引擎生成自己所需要的代碼。看看我的實際需求,就是上述第4點,我打算封裝WMI中的大部分類,有上百個吧,都是大同小異,手寫太累,所以就想到用代碼生成器,這里和Xcoder唯一的區別是,要生成代碼的信息不是數據庫架構信息,而是自己手動輸入一些元數據信息,比如下面
Win32_Keyboard
WMI class語法結構:
View Code
1
class Win32_Keyboard : CIM_Keyboard
2 {
3 uint16 Availability;
4 string Caption;
5 uint32 ConfigManagerErrorCode;
6 boolean ConfigManagerUserConfig;
7 string CreationClassName;
8 string Description;
9 string DeviceID;
10 boolean ErrorCleared;
11 string ErrorDescription;
12 datetime InstallDate;
13 boolean IsLocked;
14 uint32 LastErrorCode;
15 string Layout;
16 string Name;
17 uint16 NumberOfFunctionKeys;
18 uint16 Password;
19 string PNPDeviceID;
20 uint16 PowerManagementCapabilities[];
21 boolean PowerManagementSupported;
22 string Status;
23 uint16 StatusInfo;
24 string SystemCreationClassName;
25 string SystemName;
26 };
2 {
3 uint16 Availability;
4 string Caption;
5 uint32 ConfigManagerErrorCode;
6 boolean ConfigManagerUserConfig;
7 string CreationClassName;
8 string Description;
9 string DeviceID;
10 boolean ErrorCleared;
11 string ErrorDescription;
12 datetime InstallDate;
13 boolean IsLocked;
14 uint32 LastErrorCode;
15 string Layout;
16 string Name;
17 uint16 NumberOfFunctionKeys;
18 uint16 Password;
19 string PNPDeviceID;
20 uint16 PowerManagementCapabilities[];
21 boolean PowerManagementSupported;
22 string Status;
23 uint16 StatusInfo;
24 string SystemCreationClassName;
25 string SystemName;
26 };
就是這個基本結構,但是封裝WMI類的時候,參考了Newlife.WMI中的封裝,會有屬性和字段名稱常量等東西。這個類還算比較簡單的,就有23個字段,要是長一點50多個字段,那用手寫,大家想一下工作量。呵呵。1.知道了需求,看看怎么實現。其實要傳入到模板的參數就是上面的類的結構,包括類型和名稱,先用一個Dictionary把這些字段信息存儲起來,key為字段名稱,value為類型。代碼如下,我是在Winform中粘貼上述文本,然后程序分析,得到Dictionary。
View Code
1
///
<summary>
2 /// 獲取元數據,注意要進行類型轉換,value
3 /// </summary>
4 /// <returns></returns>
5 private Dictionary< string, object> GetData()
6 {
7 Dictionary<String, Object> data = new Dictionary<String, Object>();
8 string[] text = txtOriginText.Text.Trim().Replace( " \r\n ", "").Split( ' ; '); // 分割符為;號
9 foreach ( var item in text )
10 {
11 string[] element = item.Trim ().Split( new[]{ ' '}, StringSplitOptions.RemoveEmptyEntries);
12 if (element .Length == 2)
13 {
14 data.Add(element[ 1].Trim(), GetNewType (element[ 0].Trim()));
15 }
16 }
17 return data;
18 }
2 /// 獲取元數據,注意要進行類型轉換,value
3 /// </summary>
4 /// <returns></returns>
5 private Dictionary< string, object> GetData()
6 {
7 Dictionary<String, Object> data = new Dictionary<String, Object>();
8 string[] text = txtOriginText.Text.Trim().Replace( " \r\n ", "").Split( ' ; '); // 分割符為;號
9 foreach ( var item in text )
10 {
11 string[] element = item.Trim ().Split( new[]{ ' '}, StringSplitOptions.RemoveEmptyEntries);
12 if (element .Length == 2)
13 {
14 data.Add(element[ 1].Trim(), GetNewType (element[ 0].Trim()));
15 }
16 }
17 return data;
18 }
2.有了這些數據,就可以生成我所需要的代碼了嗎?是的,千真萬確,看看代碼,也不得不說一個小問題,考慮時間和代價,這個代碼生成並不是十全十美的,因為有一些枚舉類型,並沒有處理,這樣會出錯,只能手動修改,當然也可以在模板中處理,只是麻煩一些,為了這5%的事情去浪費95%的精力不值得,手動也快。
View Code
1 Dictionary<String, Object> data = GetData();
2 // 將元數據添加到data中去,字段名稱作為key,字段類型為 value
3 // 添加命名空間
4 data.Add( " NameSpace ", txtNameSpace.Text);
5 // 添加類名稱
6 data.Add( " ClassName ", txtClassName.Text);
7 // 需要讀入模板內容
8 string tempContent = File.ReadAllText( " WMI模板.cs ");
9 // 調用生成代碼,傳入模板名稱和數據信息Dictionary
10 string content = Template.ProcessTemplate(tempContent, data);
11 // 獲取生成文件的存儲地址
12 String dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
13 txtOutputFolder.Text.Trim (),txtClassName.Text .Trim ()+ " .cs ");
14 StreamWriter fs = File.CreateText(dir );
15 fs.Write(content);
16 fs.Close();
2 // 將元數據添加到data中去,字段名稱作為key,字段類型為 value
3 // 添加命名空間
4 data.Add( " NameSpace ", txtNameSpace.Text);
5 // 添加類名稱
6 data.Add( " ClassName ", txtClassName.Text);
7 // 需要讀入模板內容
8 string tempContent = File.ReadAllText( " WMI模板.cs ");
9 // 調用生成代碼,傳入模板名稱和數據信息Dictionary
10 string content = Template.ProcessTemplate(tempContent, data);
11 // 獲取生成文件的存儲地址
12 String dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
13 txtOutputFolder.Text.Trim (),txtClassName.Text .Trim ()+ " .cs ");
14 StreamWriter fs = File.CreateText(dir );
15 fs.Write(content);
16 fs.Close();
再來看看,我生成的內容是什么,看看內容吧(模板下一篇再寫),這一篇主要介紹模板的調用方法和過程:
生成的代碼太多,我分為2個部分吧:
View Code
View Code
1
public
class Win32_Keyboard :WMIBase
2 {
3 #region 字段定義
4 private UInt16 _availability ;
5 private string _caption ;
6 private UInt32 _configmanagererrorcode ;
7 private bool _configmanageruserconfig ;
8 private string _creationclassname ;
9 private string _description ;
10 private string _deviceid ;
11 private bool _errorcleared ;
12 private string _errordescription ;
13 private DateTime _installdate ;
14 private bool _islocked ;
15 private UInt32 _lasterrorcode ;
16 private string _layout ;
17 private string _name ;
18 private UInt16 _numberoffunctionkeys ;
19 private UInt16 _password ;
20 private string _pnpdeviceid ;
21 private UInt16 _powermanagementcapabilities[] ;
22 private bool _powermanagementsupported ;
23 private string _status ;
24 private UInt16 _statusinfo ;
25 private string _systemcreationclassname ;
26 private string _systemname ;
27 private NewLife.WMI.Entities _namespace ;
28 private Win32_Keyboard _classname ;
29 private ManagementObject _mo;
30 #endregion
31
32 #region 初始化
33 public Win32_Keyboard (ManagementObject managementObject)
34 {
35 this._mo = managementObject;
36 this.Win32_Keyboard _Init();
37 }
38 private void Win32_Keyboard _Init()
39 {
40 this._availability = GetPropStr(_mo, " Availability ");
41 this._caption = GetPropStr(_mo, " Caption ");
42 this._configmanagererrorcode = GetPropStr(_mo, " ConfigManagerErrorCode ");
43 this._configmanageruserconfig = GetPropStr(_mo, " ConfigManagerUserConfig ");
44 this._creationclassname = GetPropStr(_mo, " CreationClassName ");
45 this._description = GetPropStr(_mo, " Description ");
46 this._deviceid = GetPropStr(_mo, " DeviceID ");
47 this._errorcleared = GetPropStr(_mo, " ErrorCleared ");
48 this._errordescription = GetPropStr(_mo, " ErrorDescription ");
49 this._installdate = GetPropStr(_mo, " InstallDate ");
50 this._islocked = GetPropStr(_mo, " IsLocked ");
51 this._lasterrorcode = GetPropStr(_mo, " LastErrorCode ");
52 this._layout = GetPropStr(_mo, " Layout ");
53 this._name = GetPropStr(_mo, " Name ");
54 this._numberoffunctionkeys = GetPropStr(_mo, " NumberOfFunctionKeys ");
55 this._password = GetPropStr(_mo, " Password ");
56 this._pnpdeviceid = GetPropStr(_mo, " PNPDeviceID ");
57 this._powermanagementcapabilities[] = GetPropStr(_mo, " PowerManagementCapabilities[] ");
58 this._powermanagementsupported = GetPropStr(_mo, " PowerManagementSupported ");
59 this._status = GetPropStr(_mo, " Status ");
60 this._statusinfo = GetPropStr(_mo, " StatusInfo ");
61 this._systemcreationclassname = GetPropStr(_mo, " SystemCreationClassName ");
62 this._systemname = GetPropStr(_mo, " SystemName ");
63 this._namespace = GetPropStr(_mo, " NameSpace ");
64 this._classname = GetPropStr(_mo, " ClassName ");
65 }
66 #endregion
2 {
3 #region 字段定義
4 private UInt16 _availability ;
5 private string _caption ;
6 private UInt32 _configmanagererrorcode ;
7 private bool _configmanageruserconfig ;
8 private string _creationclassname ;
9 private string _description ;
10 private string _deviceid ;
11 private bool _errorcleared ;
12 private string _errordescription ;
13 private DateTime _installdate ;
14 private bool _islocked ;
15 private UInt32 _lasterrorcode ;
16 private string _layout ;
17 private string _name ;
18 private UInt16 _numberoffunctionkeys ;
19 private UInt16 _password ;
20 private string _pnpdeviceid ;
21 private UInt16 _powermanagementcapabilities[] ;
22 private bool _powermanagementsupported ;
23 private string _status ;
24 private UInt16 _statusinfo ;
25 private string _systemcreationclassname ;
26 private string _systemname ;
27 private NewLife.WMI.Entities _namespace ;
28 private Win32_Keyboard _classname ;
29 private ManagementObject _mo;
30 #endregion
31
32 #region 初始化
33 public Win32_Keyboard (ManagementObject managementObject)
34 {
35 this._mo = managementObject;
36 this.Win32_Keyboard _Init();
37 }
38 private void Win32_Keyboard _Init()
39 {
40 this._availability = GetPropStr(_mo, " Availability ");
41 this._caption = GetPropStr(_mo, " Caption ");
42 this._configmanagererrorcode = GetPropStr(_mo, " ConfigManagerErrorCode ");
43 this._configmanageruserconfig = GetPropStr(_mo, " ConfigManagerUserConfig ");
44 this._creationclassname = GetPropStr(_mo, " CreationClassName ");
45 this._description = GetPropStr(_mo, " Description ");
46 this._deviceid = GetPropStr(_mo, " DeviceID ");
47 this._errorcleared = GetPropStr(_mo, " ErrorCleared ");
48 this._errordescription = GetPropStr(_mo, " ErrorDescription ");
49 this._installdate = GetPropStr(_mo, " InstallDate ");
50 this._islocked = GetPropStr(_mo, " IsLocked ");
51 this._lasterrorcode = GetPropStr(_mo, " LastErrorCode ");
52 this._layout = GetPropStr(_mo, " Layout ");
53 this._name = GetPropStr(_mo, " Name ");
54 this._numberoffunctionkeys = GetPropStr(_mo, " NumberOfFunctionKeys ");
55 this._password = GetPropStr(_mo, " Password ");
56 this._pnpdeviceid = GetPropStr(_mo, " PNPDeviceID ");
57 this._powermanagementcapabilities[] = GetPropStr(_mo, " PowerManagementCapabilities[] ");
58 this._powermanagementsupported = GetPropStr(_mo, " PowerManagementSupported ");
59 this._status = GetPropStr(_mo, " Status ");
60 this._statusinfo = GetPropStr(_mo, " StatusInfo ");
61 this._systemcreationclassname = GetPropStr(_mo, " SystemCreationClassName ");
62 this._systemname = GetPropStr(_mo, " SystemName ");
63 this._namespace = GetPropStr(_mo, " NameSpace ");
64 this._classname = GetPropStr(_mo, " ClassName ");
65 }
66 #endregion
第二個部分,和上面是一個文件啊:
View Code
1
#region 屬性-默認只讀,其他手動修改
2
3 /// <summary></summary>
4 public UInt16 Availability
5 {
6 get{ return this._availability;}
7 }
8
9 /// <summary></summary>
10 public string Caption
11 {
12 get{ return this._caption;}
13 }
14
15 /// <summary></summary>
16 public UInt32 ConfigManagerErrorCode
17 {
18 get{ return this._configmanagererrorcode;}
19 }
20
21 /// <summary></summary>
22 public bool ConfigManagerUserConfig
23 {
24 get{ return this._configmanageruserconfig;}
25 }
26
27 /// <summary></summary>
28 public string CreationClassName
29 {
30 get{ return this._creationclassname;}
31 }
32
33 /// <summary></summary>
34 public string Description
35 {
36 get{ return this._description;}
37 }
38
39 /// <summary></summary>
40 public string DeviceID
41 {
42 get{ return this._deviceid;}
43 }
44
45 /// <summary></summary>
46 public bool ErrorCleared
47 {
48 get{ return this._errorcleared;}
49 }
50
51 /// <summary></summary>
52 public string ErrorDescription
53 {
54 get{ return this._errordescription;}
55 }
56
57 /// <summary></summary>
58 public DateTime InstallDate
59 {
60 get{ return this._installdate;}
61 }
62
63 /// <summary></summary>
64 public bool IsLocked
65 {
66 get{ return this._islocked;}
67 }
68
69 /// <summary></summary>
70 public UInt32 LastErrorCode
71 {
72 get{ return this._lasterrorcode;}
73 }
74
75 /// <summary></summary>
76 public string Layout
77 {
78 get{ return this._layout;}
79 }
80
81 /// <summary></summary>
82 public string Name
83 {
84 get{ return this._name;}
85 }
86
87 /// <summary></summary>
88 public UInt16 NumberOfFunctionKeys
89 {
90 get{ return this._numberoffunctionkeys;}
91 }
92
93 /// <summary></summary>
94 public UInt16 Password
95 {
96 get{ return this._password;}
97 }
98
99 /// <summary></summary>
100 public string PNPDeviceID
101 {
102 get{ return this._pnpdeviceid;}
103 }
104
105 /// <summary></summary>
106 public UInt16 PowerManagementCapabilities[]
107 {
108 get{ return this._powermanagementcapabilities[];}
109 }
110
111 /// <summary></summary>
112 public bool PowerManagementSupported
113 {
114 get{ return this._powermanagementsupported;}
115 }
116
117 /// <summary></summary>
118 public string Status
119 {
120 get{ return this._status;}
121 }
122
123 /// <summary></summary>
124 public UInt16 StatusInfo
125 {
126 get{ return this._statusinfo;}
127 }
128
129 /// <summary></summary>
130 public string SystemCreationClassName
131 {
132 get{ return this._systemcreationclassname;}
133 }
134
135 /// <summary></summary>
136 public string SystemName
137 {
138 get{ return this._systemname;}
139 }
140
141 /// <summary></summary>
142 public NewLife.WMI.Entities NameSpace
143 {
144 get{ return this._namespace;}
145 }
146
147 /// <summary></summary>
148 public Win32_Keyboard ClassName
149 {
150 get{ return this._classname;}
151 }
152
153 #endregion
154
155 #region 獲取/設置 字段值
156 /// <summary>
157 /// 獲取/設置 字段值。
158 /// 一個索引,基類使用反射實現。
159 /// 派生實體類可重寫該索引,以避免反射帶來的性能損耗
160 /// </summary>
161 /// <param name="name"> 字段名 </param>
162 public Object this[String name]
163 {
164 get{
165 switch (name)
166 {
167 case " Availability " : return _availability;
168 case " Caption " : return _caption;
169 case " ConfigManagerErrorCode " : return _configmanagererrorcode;
170 case " ConfigManagerUserConfig " : return _configmanageruserconfig;
171 case " CreationClassName " : return _creationclassname;
172 case " Description " : return _description;
173 case " DeviceID " : return _deviceid;
174 case " ErrorCleared " : return _errorcleared;
175 case " ErrorDescription " : return _errordescription;
176 case " InstallDate " : return _installdate;
177 case " IsLocked " : return _islocked;
178 case " LastErrorCode " : return _lasterrorcode;
179 case " Layout " : return _layout;
180 case " Name " : return _name;
181 case " NumberOfFunctionKeys " : return _numberoffunctionkeys;
182 case " Password " : return _password;
183 case " PNPDeviceID " : return _pnpdeviceid;
184 case " PowerManagementCapabilities[] " : return _powermanagementcapabilities[];
185 case " PowerManagementSupported " : return _powermanagementsupported;
186 case " Status " : return _status;
187 case " StatusInfo " : return _statusinfo;
188 case " SystemCreationClassName " : return _systemcreationclassname;
189 case " SystemName " : return _systemname;
190 case " NameSpace " : return _namespace;
191 case " ClassName " : return _classname;
192 default: return "" ;
193 } }
194 }
195 #endregion
196
197 #region 字段名
198 public class _
199 {
200 /// <summary></summary>
201 public static readonly string Availability = " Availability ";
202 /// <summary></summary>
203 public static readonly string Caption = " Caption ";
204 /// <summary></summary>
205 public static readonly string ConfigManagerErrorCode = " ConfigManagerErrorCode ";
206 /// <summary></summary>
207 public static readonly string ConfigManagerUserConfig = " ConfigManagerUserConfig ";
208 /// <summary></summary>
209 public static readonly string CreationClassName = " CreationClassName ";
210 /// <summary></summary>
211 public static readonly string Description = " Description ";
212 /// <summary></summary>
213 public static readonly string DeviceID = " DeviceID ";
214 /// <summary></summary>
215 public static readonly string ErrorCleared = " ErrorCleared ";
216 /// <summary></summary>
217 public static readonly string ErrorDescription = " ErrorDescription ";
218 /// <summary></summary>
219 public static readonly string InstallDate = " InstallDate ";
220 /// <summary></summary>
221 public static readonly string IsLocked = " IsLocked ";
222 /// <summary></summary>
223 public static readonly string LastErrorCode = " LastErrorCode ";
224 /// <summary></summary>
225 public static readonly string Layout = " Layout ";
226 /// <summary></summary>
227 public static readonly string Name = " Name ";
228 /// <summary></summary>
229 public static readonly string NumberOfFunctionKeys = " NumberOfFunctionKeys ";
230 /// <summary></summary>
231 public static readonly string Password = " Password ";
232 /// <summary></summary>
233 public static readonly string PNPDeviceID = " PNPDeviceID ";
234 /// <summary></summary>
235 public static readonly string PowerManagementCapabilities[] = " PowerManagementCapabilities[] ";
236 /// <summary></summary>
237 public static readonly string PowerManagementSupported = " PowerManagementSupported ";
238 /// <summary></summary>
239 public static readonly string Status = " Status ";
240 /// <summary></summary>
241 public static readonly string StatusInfo = " StatusInfo ";
242 /// <summary></summary>
243 public static readonly string SystemCreationClassName = " SystemCreationClassName ";
244 /// <summary></summary>
245 public static readonly string SystemName = " SystemName ";
246 /// <summary></summary>
247 public static readonly string NameSpace = " NameSpace ";
248 /// <summary></summary>
249 public static readonly string ClassName = " ClassName ";
250 }
251 #endregion
2
3 /// <summary></summary>
4 public UInt16 Availability
5 {
6 get{ return this._availability;}
7 }
8
9 /// <summary></summary>
10 public string Caption
11 {
12 get{ return this._caption;}
13 }
14
15 /// <summary></summary>
16 public UInt32 ConfigManagerErrorCode
17 {
18 get{ return this._configmanagererrorcode;}
19 }
20
21 /// <summary></summary>
22 public bool ConfigManagerUserConfig
23 {
24 get{ return this._configmanageruserconfig;}
25 }
26
27 /// <summary></summary>
28 public string CreationClassName
29 {
30 get{ return this._creationclassname;}
31 }
32
33 /// <summary></summary>
34 public string Description
35 {
36 get{ return this._description;}
37 }
38
39 /// <summary></summary>
40 public string DeviceID
41 {
42 get{ return this._deviceid;}
43 }
44
45 /// <summary></summary>
46 public bool ErrorCleared
47 {
48 get{ return this._errorcleared;}
49 }
50
51 /// <summary></summary>
52 public string ErrorDescription
53 {
54 get{ return this._errordescription;}
55 }
56
57 /// <summary></summary>
58 public DateTime InstallDate
59 {
60 get{ return this._installdate;}
61 }
62
63 /// <summary></summary>
64 public bool IsLocked
65 {
66 get{ return this._islocked;}
67 }
68
69 /// <summary></summary>
70 public UInt32 LastErrorCode
71 {
72 get{ return this._lasterrorcode;}
73 }
74
75 /// <summary></summary>
76 public string Layout
77 {
78 get{ return this._layout;}
79 }
80
81 /// <summary></summary>
82 public string Name
83 {
84 get{ return this._name;}
85 }
86
87 /// <summary></summary>
88 public UInt16 NumberOfFunctionKeys
89 {
90 get{ return this._numberoffunctionkeys;}
91 }
92
93 /// <summary></summary>
94 public UInt16 Password
95 {
96 get{ return this._password;}
97 }
98
99 /// <summary></summary>
100 public string PNPDeviceID
101 {
102 get{ return this._pnpdeviceid;}
103 }
104
105 /// <summary></summary>
106 public UInt16 PowerManagementCapabilities[]
107 {
108 get{ return this._powermanagementcapabilities[];}
109 }
110
111 /// <summary></summary>
112 public bool PowerManagementSupported
113 {
114 get{ return this._powermanagementsupported;}
115 }
116
117 /// <summary></summary>
118 public string Status
119 {
120 get{ return this._status;}
121 }
122
123 /// <summary></summary>
124 public UInt16 StatusInfo
125 {
126 get{ return this._statusinfo;}
127 }
128
129 /// <summary></summary>
130 public string SystemCreationClassName
131 {
132 get{ return this._systemcreationclassname;}
133 }
134
135 /// <summary></summary>
136 public string SystemName
137 {
138 get{ return this._systemname;}
139 }
140
141 /// <summary></summary>
142 public NewLife.WMI.Entities NameSpace
143 {
144 get{ return this._namespace;}
145 }
146
147 /// <summary></summary>
148 public Win32_Keyboard ClassName
149 {
150 get{ return this._classname;}
151 }
152
153 #endregion
154
155 #region 獲取/設置 字段值
156 /// <summary>
157 /// 獲取/設置 字段值。
158 /// 一個索引,基類使用反射實現。
159 /// 派生實體類可重寫該索引,以避免反射帶來的性能損耗
160 /// </summary>
161 /// <param name="name"> 字段名 </param>
162 public Object this[String name]
163 {
164 get{
165 switch (name)
166 {
167 case " Availability " : return _availability;
168 case " Caption " : return _caption;
169 case " ConfigManagerErrorCode " : return _configmanagererrorcode;
170 case " ConfigManagerUserConfig " : return _configmanageruserconfig;
171 case " CreationClassName " : return _creationclassname;
172 case " Description " : return _description;
173 case " DeviceID " : return _deviceid;
174 case " ErrorCleared " : return _errorcleared;
175 case " ErrorDescription " : return _errordescription;
176 case " InstallDate " : return _installdate;
177 case " IsLocked " : return _islocked;
178 case " LastErrorCode " : return _lasterrorcode;
179 case " Layout " : return _layout;
180 case " Name " : return _name;
181 case " NumberOfFunctionKeys " : return _numberoffunctionkeys;
182 case " Password " : return _password;
183 case " PNPDeviceID " : return _pnpdeviceid;
184 case " PowerManagementCapabilities[] " : return _powermanagementcapabilities[];
185 case " PowerManagementSupported " : return _powermanagementsupported;
186 case " Status " : return _status;
187 case " StatusInfo " : return _statusinfo;
188 case " SystemCreationClassName " : return _systemcreationclassname;
189 case " SystemName " : return _systemname;
190 case " NameSpace " : return _namespace;
191 case " ClassName " : return _classname;
192 default: return "" ;
193 } }
194 }
195 #endregion
196
197 #region 字段名
198 public class _
199 {
200 /// <summary></summary>
201 public static readonly string Availability = " Availability ";
202 /// <summary></summary>
203 public static readonly string Caption = " Caption ";
204 /// <summary></summary>
205 public static readonly string ConfigManagerErrorCode = " ConfigManagerErrorCode ";
206 /// <summary></summary>
207 public static readonly string ConfigManagerUserConfig = " ConfigManagerUserConfig ";
208 /// <summary></summary>
209 public static readonly string CreationClassName = " CreationClassName ";
210 /// <summary></summary>
211 public static readonly string Description = " Description ";
212 /// <summary></summary>
213 public static readonly string DeviceID = " DeviceID ";
214 /// <summary></summary>
215 public static readonly string ErrorCleared = " ErrorCleared ";
216 /// <summary></summary>
217 public static readonly string ErrorDescription = " ErrorDescription ";
218 /// <summary></summary>
219 public static readonly string InstallDate = " InstallDate ";
220 /// <summary></summary>
221 public static readonly string IsLocked = " IsLocked ";
222 /// <summary></summary>
223 public static readonly string LastErrorCode = " LastErrorCode ";
224 /// <summary></summary>
225 public static readonly string Layout = " Layout ";
226 /// <summary></summary>
227 public static readonly string Name = " Name ";
228 /// <summary></summary>
229 public static readonly string NumberOfFunctionKeys = " NumberOfFunctionKeys ";
230 /// <summary></summary>
231 public static readonly string Password = " Password ";
232 /// <summary></summary>
233 public static readonly string PNPDeviceID = " PNPDeviceID ";
234 /// <summary></summary>
235 public static readonly string PowerManagementCapabilities[] = " PowerManagementCapabilities[] ";
236 /// <summary></summary>
237 public static readonly string PowerManagementSupported = " PowerManagementSupported ";
238 /// <summary></summary>
239 public static readonly string Status = " Status ";
240 /// <summary></summary>
241 public static readonly string StatusInfo = " StatusInfo ";
242 /// <summary></summary>
243 public static readonly string SystemCreationClassName = " SystemCreationClassName ";
244 /// <summary></summary>
245 public static readonly string SystemName = " SystemName ";
246 /// <summary></summary>
247 public static readonly string NameSpace = " NameSpace ";
248 /// <summary></summary>
249 public static readonly string ClassName = " ClassName ";
250 }
251 #endregion
上面是主要代碼,下面看看我用主要代碼做的一個簡單界面,以及運行時候的效果。下一篇將寫一下,上面代碼生成的模板如何寫,以及要注意的事項。
我用Winform做的WMI代碼生成界面,比較丑陋啊,但是功能不減,呵呵。
| -- |
