生成代码语句(可以理解为带分号结束的代码,除了if,for这些)所使用到的类,都是从CodeStatement,因此,要知道.NET类库所提供的类能生成哪些代码语句,从MSDN参考文档中找到CodeStatement类,然后,还是老方法,查看它的继承关系。就可以“一览众山小”。
我还是坚持老方法,不会从理论层面去介绍每个类如何使用,因为,我们要学习的内容,在MSDN上是绝对能找到的,因此,实在不应该也没有必要拿MSDN上的东西来抄一遍。而且,实例对人的感染力远远超越枯燥的理论。因而,老规矩,一起来动手实践吧。
一、生成注释代码
注释,不知道大家平时写代码有没有写注释的习惯,其实,这是个好习惯,不一定要每句代码都写注释,在合适的位置,写上必要的注释,是灰常重要,一来也方便你自己日后查看,二来,别人阅读你的码子会轻松一些。
注释,就是带 // 开头的语句,当然对于.NET语言(C#或VB)有XML文档注释,可以生成文档级别的注释,形如 /// 开头的语句。
CodeCommentStatement类可以生成以 // 开头或以 /// 开头的代码(C#)。
/*
* 本示例演示如何生成注释代码
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Example_1
{
class Program
{
static void Main(string[] args)
{
CodeTypeDeclaration Myclass = new CodeTypeDeclaration("ClassA");
// 生成XML注释
Myclass.Comments.Add(new CodeCommentStatement("<summary>", true));
Myclass.Comments.Add(new CodeCommentStatement("这是一个公共类", true));
Myclass.Comments.Add(new CodeCommentStatement("</summary>", true));
CodeDomProvider provide = CodeDomProvider.CreateProvider("cs");
provide.GenerateCodeFromType(Myclass, Console.Out, new CodeGeneratorOptions());
Console.ReadKey();
}
}
}
上面代码执行结果如下图所示。
二、生成赋值语句
赋值语句就更好说了,如a = 350; 赋值语句分为两部分:左 And 右,中间是一个=号。赋值语句主要通过CodeAssignStatement类来生成,例如
/*
* 本例演示如何生成赋值语句
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Example_2
{
class Program
{
static void Main(string[] args)
{
CodeAssignStatement as1 = new CodeAssignStatement(
new CodeVariableReferenceExpression("a"),
new CodePrimitiveExpression(250));
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
provider.GenerateCodeFromStatement(as1, Console.Out, new CodeGeneratorOptions());
Console.ReadKey();
}
}
}
来,看看结果。
三、生成条件语句
条件语句是指if .... else.....语句,使用CodeConditionStatement类来生成,其中,Condition属性表示用于条件判断的表达式;TrueStatements属性表示当条件为真时的语句块,可以添加N条语句;FalseStatements属性表示当判断条件为假时的代码块。
/*
* 示例演示如何生成条件语句
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Example_3
{
class Program
{
static void Main(string[] args)
{
CodeConditionStatement cdtstm = new CodeConditionStatement();
// 条件
cdtstm.Condition = new CodeBinaryOperatorExpression(
new CodePrimitiveExpression(100), CodeBinaryOperatorType.LessThanOrEqual,
new CodePrimitiveExpression(300));
// 条件为true时
cdtstm.TrueStatements.Add(new CodeCommentStatement("正常啊"));
// 条件为false
cdtstm.FalseStatements.Add(new CodeCommentStatement("当条件不成立时"));
CodeDomProvider provider = CodeDomProvider.CreateProvider("cs");
provider.GenerateCodeFromStatement(cdtstm, Console.Out, new CodeGeneratorOptions() { BracingStyle = "C" });
Console.ReadKey();
}
}
}
执行的结果如下图所示。
四、生成for循环语句
CodeIterationStatement类可以完成该任务,InitStatement属性即为初始化,如 for(int i = 0; i >5; i--)中的 int i = 0; 就是InitStatement属性;
i > 5;为测试语句,即TestExpression属性;i-- 为每次循环执行后调用的语句,即增量语句,通过IncrementStatement属性设置。
/*
* 本示例演示生成for循环语句
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Example_4
{
class Program
{
static void Main(string[] args)
{
CodeIterationStatement MyFor = new CodeIterationStatement();
// 初始化
MyFor.InitStatement = new CodeVariableDeclarationStatement(
typeof(int), "n", new CodePrimitiveExpression(0));
// 递增条件
MyFor.IncrementStatement = new CodeAssignStatement(
new CodeVariableReferenceExpression("n"),
new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)));
// 测试表达式
MyFor.TestExpression = new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("n"), CodeBinaryOperatorType.LessThan,
new CodePrimitiveExpression(10));
// 循环语句内部
MyFor.Statements.Add(new CodeCommentStatement("这是一个for循环。"));
CodeDomProvider provider = CodeDomProvider.CreateProvider("cs");
provider.GenerateCodeFromStatement(MyFor, Console.Out, new CodeGeneratorOptions { BracingStyle = "C" });
Console.ReadKey();
}
}
}
其运行结果如图所示。
五、生成try...catch...finally语句
CodeTryCatchFinallyStatement类的TryStatements属性表示语句中try块中的语句;CatchClauses属性表示catch块中的内容;FinallyStatements属性则为finally块的语句。
/*
* 生成try...catch...finally语句
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Example_5
{
class Program
{
static void Main(string[] args)
{
CodeTryCatchFinallyStatement myTrycafly = new CodeTryCatchFinallyStatement();
// try
myTrycafly.TryStatements.Add(new CodeCommentStatement("try块内部"));
// catch
myTrycafly.CatchClauses.Add(new CodeCatchClause(
"ex", new CodeTypeReference(typeof(Exception)),
new CodeCommentStatement("catch块内部")));
// finally
myTrycafly.FinallyStatements.Add(new CodeCommentStatement("finally块内部"));
CodeDomProvider provider = CodeDomProvider.CreateProvider("cs");
provider.GenerateCodeFromStatement(myTrycafly, Console.Out, new CodeGeneratorOptions { BracingStyle = "C" });
Console.ReadKey();
}
}
}
下面是运行的结果。