上一篇中有几个例子已经比较复杂地运用了CodeDom,但细分下来还是要总结一下。
下面是常用的一些代码例子:
//
// Main方法
//
CodeEntryPointMethod main = new CodeEntryPointMethod();
main.Comments.Add(new CodeCommentStatement("Main入口函数"));
main.Name = "Main";
main.Attributes = MemberAttributes.Public | MemberAttributes.Static;
main.ReturnType = new CodeTypeReference("System.Void");
main.CustomAttributes.Add(new CodeAttributeDeclaration("System.STAThreadAttribute"));
main.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(System.Windows.Forms.Application)), "Run"), new CodeExpression[] {
new CodeObjectCreateExpression(new CodeTypeReference("Form"))
}));
myDesignerClass.Members.Add(main);
//
// 字段
//
CodeMemberField cmf = new CodeMemberField("System.String", "myName");
cmf.Attributes = MemberAttributes.Private;
cmf.Comments.Add(new CodeCommentStatement("私有字段"));
myDesignerClass.Members.Add(cmf);
//
// 属性
//
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = "MyAttribute";
prop.Comments.Add(new CodeCommentStatement("属性"));
prop.Attributes = MemberAttributes.Public;
prop.Type = new CodeTypeReference("System.String");
prop.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "sName")));
prop.SetStatements.Add(new CodeAssignStatement(
new CodePropertyReferenceExpression(
new CodeThisReferenceExpression(), "sName"),
new CodeArgumentReferenceExpression("value")));
myDesignerClass.Members.Add(prop);
//
//方法
//
CodeMemberMethod cmm = new CodeMemberMethod();
cmm.Name = "MyFunction";
cmm.Comments.Add(new CodeCommentStatement("成员方法"));
cmm.ReturnType = new CodeTypeReference("System.Boolean");
cmm.Attributes = MemberAttributes.Public;
myDesignerClass.Members.Add(cmm);
//方法的参数
CodeParameterDeclarationExpression paraDeclaration = new CodeParameterDeclarationExpression("System.String", "name");
cmm.Parameters.Add(paraDeclaration);
//参数声明表达式
CodeParameterDeclarationExpression paraInt = new CodeParameterDeclarationExpression("System.Int32", "index");
CodeParameterDeclarationExpression paraStr = new CodeParameterDeclarationExpression(new CodeTypeReference("System.String"), "name");
cmm.Parameters.Add(paraInt);
cmm.Parameters.Add(paraStr);
//基元数据表达式,一般用来表示常量
CodeExpression constStr = new CodePrimitiveExpression("Hello World");
cmm.Statements.Add(constStr);
//this引用表达式
CodeExpression thisRef = new CodeThisReferenceExpression();
cmm.Statements.Add(thisRef);
//引用父类表达式
CodeExpression baseRef = new CodeBaseReferenceExpression();
cmm.Statements.Add(baseRef);
//运算表达式
CodeExpression calc = new CodeBinaryOperatorExpression(new CodePrimitiveExpression(3), CodeBinaryOperatorType.Multiply, new CodePrimitiveExpression(4));
cmm.Statements.Add(calc);
//对象表达式
CodeExpression objValue = new CodeObjectCreateExpression(new CodeTypeReference("People"), new CodeExpression[] { new CodePrimitiveExpression("ZhangSan") });
cmm.Statements.Add(objValue);
//数组表达式
CodeExpression strArr = new CodeArrayCreateExpression("System.String", new CodeExpression[] { new CodePrimitiveExpression("ZhangSan"), new CodePrimitiveExpression("LiSi") });
cmm.Statements.Add(strArr);
CodeExpression intArr = new CodeArrayCreateExpression("System.Int32", 4);
cmm.Statements.Add(intArr);
//引用类型表达式
CodeExpression obj = new CodeTypeReferenceExpression("myObj");
cmm.Statements.Add(obj);
//索引表达式
CodeExpression objIdx = new CodeIndexerExpression(new CodeTypeReferenceExpression("myObj"), new CodeSnippetExpression("index"));
cmm.Statements.Add(objIdx);
//数组索引表达式
CodeExpression arrIdx = new CodeArrayIndexerExpression(new CodeTypeReferenceExpression("MyArr"), new CodeSnippetExpression("2"));
cmm.Statements.Add(arrIdx);
//局部变量表达式
CodeExpression localVal = new CodeVariableReferenceExpression("tempStr");
cmm.Statements.Add(localVal);
//引用字段表达式
CodeExpression field = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("obj"), "mName");
cmm.Statements.Add(field);
CodeExpression prop = new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("obj"), "Prop");
cmm.Statements.Add(prop);
//强制类型转换表达式
CodeExpression conv = new CodeCastExpression(new CodeTypeReference("TargetClass"), new CodePrimitiveExpression("Obj"));
cmm.Statements.Add(conv);
//方法调用表达式
CodeExpression callMethod = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello CodeDom!"));
cmm.Statements.Add(callMethod);
CodeExpression callMethod2 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("List"), "CopyTo", new CodeExpression[] { new CodeSnippetExpression("array"), new CodeSnippetExpression("index") });
cmm.Statements.Add(callMethod2);
//注释语句
CodeStatement comment = new CodeCommentStatement("定义变量并赋初值");
cmm.Statements.Add(comment);
//变量声明语句
CodeStatement strDec = new CodeVariableDeclarationStatement("System.String", "user1", new CodePrimitiveExpression("ZhangSan"));
cmm.Statements.Add(strDec);
CodeStatement strDec2 = new CodeVariableDeclarationStatement("System.String", "user2");
cmm.Statements.Add(strDec2);
//赋值语句
CodeStatement assign = new CodeAssignStatement(new CodeVariableReferenceExpression("user2"), new CodePrimitiveExpression("LiSi"));
cmm.Statements.Add(assign);
CodeStatement dtDec = new CodeVariableDeclarationStatement("System.DateTime", "dt");
cmm.Statements.Add(dtDec);
CodeStatement dtAssign = new CodeAssignStatement(new CodeVariableReferenceExpression("dt"), new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("System.DateTime"), "Now"));
cmm.Statements.Add(dtAssign);
//组合表达式语句
CodeStatement stmt = new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodeBinaryOperatorExpression(new CodePrimitiveExpression("Hello."), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("sName"))));
cmm.Statements.Add(stmt);
//函数返回语句
CodeStatement ret = new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("List"), "Contains", new CodeExpression[] { new CodeSnippetExpression("value") }));
cmm.Statements.Add(ret);
CodeStatement ret2 = new CodeMethodReturnStatement(new CodeSnippetExpression("true"));
cmm.Statements.Add(ret2);
//
//条件语句,仅支持if...else...不支持if...else if...else...
//
CodeConditionStatement cstif = new CodeConditionStatement();
//设置条件表达式
cstif.Condition = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("sName"), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression("ZhangSan"));
//条件为真时的语句
cstif.TrueStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodeExpression[] { new CodePrimitiveExpression("Hello, ZhangSan") })));
//条件为假时的语句
cstif.FalseStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodeExpression[] { new CodePrimitiveExpression("You are not ZhangSan") })));
cmm.Statements.Add(cstif);
//
//循环语句,仅支持for
//
cmm.Statements.Add(new CodeVariableDeclarationStatement("System.Int32", "nSum", new CodePrimitiveExpression(0)));
//生成循环语句
CodeIterationStatement cstfor = new CodeIterationStatement();
//初始声明整型变量i,并初始化为1
cstfor.InitStatement = new CodeVariableDeclarationStatement("System.Int32", "i", new CodePrimitiveExpression(1));
//设置增量语句,每次将变量i加1
cstfor.IncrementStatement = new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)));
//条件表达式
cstfor.TestExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThanOrEqual, new CodePrimitiveExpression(100));
//循环体
cstfor.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("nSum"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("nSum"), CodeBinaryOperatorType.Add, new CodeVariableReferenceExpression("i"))));
cmm.Statements.Add(cstfor);
可生成的C#代码如下:
// Main入口函数
[System.STAThreadAttribute()]
public static void Main()
{
System.Windows.Forms.Application.Run(new CreateCodeTest());
}
// 私有字段
private string myName;
// 属性
public virtual string MyAttribute
{
get
{
return this.sName;
}
set
{
this.sName = value;
}
}
// 成员方法
public virtual bool MyFunction(int index, string name)
{
"Hello World";
this;
base;
(3 * 4);
new People("ZhangSan");
new string[] {
"ZhangSan",
"LiSi"};
new int[4];
myObj;
myObj[index];
MyArr[2];
tempStr;
obj.mName;
obj.Prop;
((TargetClass)("Obj"));
Console.WriteLine("Hello CodeDom!");
List.CopyTo(array, index);
// 定义变量并赋初值
string user1 = "ZhangSan";
string user2;
user2 = "LiSi";
System.DateTime dt;
dt = System.DateTime.Now;
Console.WriteLine(("Hello." + sName));
return List.Contains(value);
return true;
if ((sName == "ZhangSan"))
{
System.Console.WriteLine("Hello, ZhangSan");
}
else
{
System.Console.WriteLine("You are not ZhangSan");
}
int nSum = 0;
for (int i = 1; (i <= 100); i = (i + 1))
{
nSum = (nSum + i);
}
}
上面提到仅支持for循环以及if…else…,至于while循环可写成for(;;)形式,else里面又可以放if…else…。
当然也有说法可以使用CodeSnippetExpression写出while循环以及if…else if…else这样的条件语句,但其实已失去了所谓的“CodeDom”精神。
究竟是为什么才使用CodeDom的呢?
————————————————
版权声明:本文为CSDN博主「莱昂纳多达芬奇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/davinciyxw/article/details/7767103