1using System;
2using System.CodeDom;
3using System.CodeDom.Compiler;
4using Microsoft.CSharp;
5using System.IO;
6
7namespace CodeDomFacade
8{
9 /// <summary>
10 /// 该类封装了CodeCom相关操作,方便外部使用
11 /// </summary>
12 public class CodeDomHelper
13 {
14
15 protected CodeCompileUnit _CodeRoot;
16 protected CodeNamespace _cn;
17 protected CodeDomProvider _provider;
18 protected string _strFileName;
19 protected CodeTypeDeclaration _ctdClass;
20
21 public CodeDomHelper()
22 {
23 _CodeRoot=new CodeCompileUnit();
24 _ctdClass=new CodeTypeDeclaration();
25 }
26
27 /// <summary>
28 /// 生成代码输出到文件
29 /// </summary>
30 public void GenerateCode()
31 {
32 try
33 {
34 // Obtain an ICodeGenerator from a CodeDomProvider class.
35 ICodeGenerator gen = _provider.CreateGenerator();
36 // Create a TextWriter to a StreamWriter to an output file.
37 IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(_strFileName, false), " ");
38 // Generate source code using the code generator.
39 gen.GenerateCodeFromCompileUnit(_CodeRoot, tw, new CodeGeneratorOptions());
40 // Close the output file.
41 tw.Close();
42 }
43 catch(System.Exception e)
44 {
45 throw e;
46 }
47 }
48
49 /// <summary>
50 /// 生成C#代码
51 /// </summary>
52 public void GenerateCSharpCode()
53 {
54 _provider=new CSharpCodeProvider();//设定生成代码为C#代码
55 GenerateCode();//生成代码,写入指定文件
56 }
57 /// <summary>
58 /// 设置代码命名空间
59 /// </summary>
60 /// <param name="name">命名空间名字</param>
61 public void DefineNameSpace(string name)
62 {
63 try
64 {
65 _cn=new CodeNamespace(name);
66 _CodeRoot.Namespaces.Add(_cn);
67 }
68 catch(System.Exception e)
69 {
70 throw e;
71 }
72 }
73
74 /// <summary>
75 /// 添加引用命名空间
76 /// </summary>
77 /// <param name="name">被引用命名空间名字</param>
78 public void AddImportNameSpace(string name)
79 {
80 try
81 {
82 _cn.Imports.Add(new CodeNamespaceImport(name));
83 }
84 catch(System.Exception e)
85 {
86 throw e;
87 }
88 }
89
90 /// <summary>
91 /// 添加一个类
92 /// </summary>
93 /// <param name="classname">类名</param>
94 public void AddClass(string classname)
95 {
96 try
97 {
98 _ctdClass.Name=classname;
99 _cn.Types.Add(_ctdClass);
100 }
101 catch(System.Exception e)
102 {
103 throw e;
104 }
105 }
106
107 /// <summary>
108 /// 添加一个方法到类
109 /// </summary>
110 /// <param name="methodname">方法名</param>
111 /// <param name="returntype">返回值类型,如"System.Int32"</param>
112 /// <param name="visit">访问控制类型"public" or "private"</param>
113 public CodeMemberMethod AddMethod(string methodname,string returntype,string visit)
114 {
115 CodeMemberMethod cmm=new CodeMemberMethod();
116 try
117 {
118 cmm.Name=methodname;
119 cmm.ReturnType =new CodeTypeReference(returntype);
120 switch(visit)
121 {
122 case "public":
123 cmm.Attributes=MemberAttributes.Public|MemberAttributes.Final;
124 break;
125 case "private":
126 cmm.Attributes=MemberAttributes.Private|MemberAttributes.Final;
127 break;
128 default:
129 cmm.Attributes=MemberAttributes.Private|MemberAttributes.Final;
130 break;
131 }
132
133 _ctdClass.Members.Add(cmm);
134 }
135 catch(System.Exception e)
136 {
137 throw e;
138 }
139 return cmm;
140 }
141
142 /// <summary>
143 /// 添加带有普通的get/set操作属性
144 /// </summary>
145 /// <param name="pname">属性名</param>
146 /// <param name="returntype">返回值类型</param>
147 /// <param name="visit">访问控制类型</param>
148 /// <param name="fieldname">访问字段名</param>
149 /// <returns>返回添加的属性,待以后操作,如添加原代码段</returns>
150 public CodeMemberProperty AddProperty(string pname,string returntype,string visit,string fieldname)
151 {
152 CodeMemberProperty cmp=new CodeMemberProperty();
153 try
154 {
155 cmp.Name=pname;
156 cmp.Type=new CodeTypeReference(returntype);
157 switch(visit)
158 {
159 case "public":
160 cmp.Attributes=MemberAttributes.Public|MemberAttributes.Final;
161 break;
162 case "private":
163 cmp.Attributes=MemberAttributes.Private|MemberAttributes.Final;
164 break;
165 default:
166 cmp.Attributes=MemberAttributes.Private|MemberAttributes.Final;
167 break;
168 }
169 //添加get操作代码
170 cmp.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),fieldname)));
171 //添加set操作代码
172 cmp.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),fieldname),new CodePropertySetValueReferenceExpression()));
173 _ctdClass.Members.Add(cmp);
174 }
175 catch(System.Exception e)
176 {
177 throw e;
178 }
179 return cmp;
180 }
181
182 /// <summary>
183 /// 添加字段
184 /// </summary>
185 /// <param name="name">字段名</param>
186 /// <param name="type">字段类型</param>
187 /// <param name="visit">访问控制类型</param>
188 /// <param name="IsCreateProperty">是否生成对应属性</param>
189 public void AddField(string name,string type,string visit,bool IsCreateProperty)
190 {
191 CodeMemberField cmf=new CodeMemberField(type,name);
192 try
193 {
194 switch(visit)
195 {
196 case "public":
197 cmf.Attributes=MemberAttributes.Public|MemberAttributes.Final;
198 break;
199 case "private":
200 cmf.Attributes=MemberAttributes.Private|MemberAttributes.Final;
201 break;
202 default:
203 cmf.Attributes=MemberAttributes.Private|MemberAttributes.Final;
204 break;
205 }
206 _ctdClass.Members.Add(cmf);
207 if(IsCreateProperty)
208 {
209 if(name.IndexOf("_")>0)
210 AddProperty(name.Replace("_",""),type,"public",name);
211 else
212 AddProperty("p" + name,type,"public",name);
213 }
214 }
215 catch(System.Exception e)
216 {
217 throw e;
218 }
219 }
220
221 /// <summary>
222 /// 从指定文件获取代码
223 /// </summary>
224 /// <param name="filename">文件路径(包含名称)</param>
225 /// <returns></returns>
226 public string GetCodeFormFile(string filename)
227 {
228 try
229 {
230 StreamReader sr = new StreamReader(filename);
231 string result = sr.ReadToEnd();//读取文件内容到文本框
232 sr.Close();
233 return result;
234 }
235 catch(System.Exception e)
236 {
237 throw e;
238 return "读取文件出错";
239 }
240 }
241
242 /// <summary>
243 /// 添加原代码片断
244 /// </summary>
245 /// <param name="cmm">要添加的地方,如方法,入口点,结构等</param>
246 /// <param name="strCode">具体代码</param>
247 public void AddCodeSnippet(CodeMemberMethod cmm,string strCode)
248 {
249 try
250 {
251 CodeSnippetStatement codebody = null;
252 codebody = new CodeSnippetStatement(strCode);
253 cmm.Statements.Add(codebody);
254 }
255 catch(System.Exception e)
256 {
257 throw e;
258 }
259 }
260 相关属性
321 }
322}
323
最近在看CodeDom,发现其类层次很多,而且很复杂,所以写了一个Farade模式的类,希望在以后的开发中简化使用CodeDom,但由于时间有限,所以功能不够完善,也许要在以后使用中不断完善。
2using System.CodeDom;
3using System.CodeDom.Compiler;
4using Microsoft.CSharp;
5using System.IO;
6
7namespace CodeDomFacade
8{
9 /// <summary>
10 /// 该类封装了CodeCom相关操作,方便外部使用
11 /// </summary>
12 public class CodeDomHelper
13 {
14
15 protected CodeCompileUnit _CodeRoot;
16 protected CodeNamespace _cn;
17 protected CodeDomProvider _provider;
18 protected string _strFileName;
19 protected CodeTypeDeclaration _ctdClass;
20
21 public CodeDomHelper()
22 {
23 _CodeRoot=new CodeCompileUnit();
24 _ctdClass=new CodeTypeDeclaration();
25 }
26
27 /// <summary>
28 /// 生成代码输出到文件
29 /// </summary>
30 public void GenerateCode()
31 {
32 try
33 {
34 // Obtain an ICodeGenerator from a CodeDomProvider class.
35 ICodeGenerator gen = _provider.CreateGenerator();
36 // Create a TextWriter to a StreamWriter to an output file.
37 IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(_strFileName, false), " ");
38 // Generate source code using the code generator.
39 gen.GenerateCodeFromCompileUnit(_CodeRoot, tw, new CodeGeneratorOptions());
40 // Close the output file.
41 tw.Close();
42 }
43 catch(System.Exception e)
44 {
45 throw e;
46 }
47 }
48
49 /// <summary>
50 /// 生成C#代码
51 /// </summary>
52 public void GenerateCSharpCode()
53 {
54 _provider=new CSharpCodeProvider();//设定生成代码为C#代码
55 GenerateCode();//生成代码,写入指定文件
56 }
57 /// <summary>
58 /// 设置代码命名空间
59 /// </summary>
60 /// <param name="name">命名空间名字</param>
61 public void DefineNameSpace(string name)
62 {
63 try
64 {
65 _cn=new CodeNamespace(name);
66 _CodeRoot.Namespaces.Add(_cn);
67 }
68 catch(System.Exception e)
69 {
70 throw e;
71 }
72 }
73
74 /// <summary>
75 /// 添加引用命名空间
76 /// </summary>
77 /// <param name="name">被引用命名空间名字</param>
78 public void AddImportNameSpace(string name)
79 {
80 try
81 {
82 _cn.Imports.Add(new CodeNamespaceImport(name));
83 }
84 catch(System.Exception e)
85 {
86 throw e;
87 }
88 }
89
90 /// <summary>
91 /// 添加一个类
92 /// </summary>
93 /// <param name="classname">类名</param>
94 public void AddClass(string classname)
95 {
96 try
97 {
98 _ctdClass.Name=classname;
99 _cn.Types.Add(_ctdClass);
100 }
101 catch(System.Exception e)
102 {
103 throw e;
104 }
105 }
106
107 /// <summary>
108 /// 添加一个方法到类
109 /// </summary>
110 /// <param name="methodname">方法名</param>
111 /// <param name="returntype">返回值类型,如"System.Int32"</param>
112 /// <param name="visit">访问控制类型"public" or "private"</param>
113 public CodeMemberMethod AddMethod(string methodname,string returntype,string visit)
114 {
115 CodeMemberMethod cmm=new CodeMemberMethod();
116 try
117 {
118 cmm.Name=methodname;
119 cmm.ReturnType =new CodeTypeReference(returntype);
120 switch(visit)
121 {
122 case "public":
123 cmm.Attributes=MemberAttributes.Public|MemberAttributes.Final;
124 break;
125 case "private":
126 cmm.Attributes=MemberAttributes.Private|MemberAttributes.Final;
127 break;
128 default:
129 cmm.Attributes=MemberAttributes.Private|MemberAttributes.Final;
130 break;
131 }
132
133 _ctdClass.Members.Add(cmm);
134 }
135 catch(System.Exception e)
136 {
137 throw e;
138 }
139 return cmm;
140 }
141
142 /// <summary>
143 /// 添加带有普通的get/set操作属性
144 /// </summary>
145 /// <param name="pname">属性名</param>
146 /// <param name="returntype">返回值类型</param>
147 /// <param name="visit">访问控制类型</param>
148 /// <param name="fieldname">访问字段名</param>
149 /// <returns>返回添加的属性,待以后操作,如添加原代码段</returns>
150 public CodeMemberProperty AddProperty(string pname,string returntype,string visit,string fieldname)
151 {
152 CodeMemberProperty cmp=new CodeMemberProperty();
153 try
154 {
155 cmp.Name=pname;
156 cmp.Type=new CodeTypeReference(returntype);
157 switch(visit)
158 {
159 case "public":
160 cmp.Attributes=MemberAttributes.Public|MemberAttributes.Final;
161 break;
162 case "private":
163 cmp.Attributes=MemberAttributes.Private|MemberAttributes.Final;
164 break;
165 default:
166 cmp.Attributes=MemberAttributes.Private|MemberAttributes.Final;
167 break;
168 }
169 //添加get操作代码
170 cmp.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),fieldname)));
171 //添加set操作代码
172 cmp.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),fieldname),new CodePropertySetValueReferenceExpression()));
173 _ctdClass.Members.Add(cmp);
174 }
175 catch(System.Exception e)
176 {
177 throw e;
178 }
179 return cmp;
180 }
181
182 /// <summary>
183 /// 添加字段
184 /// </summary>
185 /// <param name="name">字段名</param>
186 /// <param name="type">字段类型</param>
187 /// <param name="visit">访问控制类型</param>
188 /// <param name="IsCreateProperty">是否生成对应属性</param>
189 public void AddField(string name,string type,string visit,bool IsCreateProperty)
190 {
191 CodeMemberField cmf=new CodeMemberField(type,name);
192 try
193 {
194 switch(visit)
195 {
196 case "public":
197 cmf.Attributes=MemberAttributes.Public|MemberAttributes.Final;
198 break;
199 case "private":
200 cmf.Attributes=MemberAttributes.Private|MemberAttributes.Final;
201 break;
202 default:
203 cmf.Attributes=MemberAttributes.Private|MemberAttributes.Final;
204 break;
205 }
206 _ctdClass.Members.Add(cmf);
207 if(IsCreateProperty)
208 {
209 if(name.IndexOf("_")>0)
210 AddProperty(name.Replace("_",""),type,"public",name);
211 else
212 AddProperty("p" + name,type,"public",name);
213 }
214 }
215 catch(System.Exception e)
216 {
217 throw e;
218 }
219 }
220
221 /// <summary>
222 /// 从指定文件获取代码
223 /// </summary>
224 /// <param name="filename">文件路径(包含名称)</param>
225 /// <returns></returns>
226 public string GetCodeFormFile(string filename)
227 {
228 try
229 {
230 StreamReader sr = new StreamReader(filename);
231 string result = sr.ReadToEnd();//读取文件内容到文本框
232 sr.Close();
233 return result;
234 }
235 catch(System.Exception e)
236 {
237 throw e;
238 return "读取文件出错";
239 }
240 }
241
242 /// <summary>
243 /// 添加原代码片断
244 /// </summary>
245 /// <param name="cmm">要添加的地方,如方法,入口点,结构等</param>
246 /// <param name="strCode">具体代码</param>
247 public void AddCodeSnippet(CodeMemberMethod cmm,string strCode)
248 {
249 try
250 {
251 CodeSnippetStatement codebody = null;
252 codebody = new CodeSnippetStatement(strCode);
253 cmm.Statements.Add(codebody);
254 }
255 catch(System.Exception e)
256 {
257 throw e;
258 }
259 }
260 相关属性
321 }
322}
323