1 由于是CodeDom些列,所以先介绍几个CodeDom表达式: 2 3 1:CodeConditionStatement:判断语句即是if(condition){} else{},看最全的那个构造函数: 4 5 public CodeConditionStatement( 6 CodeExpression condition,//条件 7 CodeStatement[] trueStatements,//为true的语句体 8 CodeStatement[] falseStatements//为false语句体 9 ) 10 2:CodeIterationStatement():表示 for 语句或语句块内的循环(使用测试表达式作为继续循环的条件): 11 在codedom中没有提高while和dowhile但是For已经够用的 12 public CodeIterationStatement( 13 CodeStatement initStatement,//for初始化 14 CodeExpression testExpression,//条件表达式 15 CodeStatement incrementStatement,//for变化体,增或减 16 CodeStatement[] statements//循环体 17 ) 18 3:CodeBinaryOperatorExpression:表示一个表达式,该表达式包含在两个表达式间进行的二进制运算, 19 public CodeBinaryOperatorExpression( 20 CodeExpression left,//表达式左边 21 CodeBinaryOperatorType op,//操作符 22 CodeExpression right//表达式右边 23 ) 24 4:CodeArrayIndexerExpression:表示对数组的索引的引用: 25 public CodeArrayIndexerExpression( 26 CodeExpression targetObject,//数组对象 27 CodeExpression[] indices//下标 28 ) 29 其他参见CodeDOM 快速参考:msdn.microsoft.com/zh-cn/library/f1dfsbhc(VS.80).aspx 30 code: 31 public CodeNamespace CreateNameSpace() 32 { 33 public CodeNamespace CreateNameSpace() 34 { 35 //Test 36 CodeMemberMethod test = new CodeMemberMethod(); 37 test.Name = "Test"; 38 test.Attributes = MemberAttributes.Public | MemberAttributes.Final; 39 test.Statements.Add(new CodeVariableDeclarationStatement(typeof(char[]), "ch", 40 new CodeMethodInvokeExpression(new CodePrimitiveExpression("+ - + - + + - -"), "ToCharArray", 41 new CodeExpression[] { }))); 42 test.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression 43 (new CodeThisReferenceExpression(), "FuHaoSanJiao"), new CodeExpression[] { new CodeVariableReferenceExpression("ch"), 44 new CodePrimitiveExpression(0) })); 45 test.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), "Read")); 46 //FuHaoSanJiao 47 CodeMemberMethod fuHaoSanJiao = new CodeMemberMethod(); 48 fuHaoSanJiao.Name = "FuHaoSanJiao"; 49 fuHaoSanJiao.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(char[])), 50 "ch")); 51 fuHaoSanJiao.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(int)), "start")); 52 fuHaoSanJiao.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), 53 "WriteLine", new CodeExpression[] { new CodeObjectCreateExpression(typeof(string), new CodeArgumentReferenceExpression("ch")) })); 54 fuHaoSanJiao.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression( 55 new CodeArgumentReferenceExpression("start"), CodeBinaryOperatorType.GreaterThanOrEqual, 56 (new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression( 57 new CodeArgumentReferenceExpression("ch"), "Length"), 58 CodeBinaryOperatorType.Divide, new CodePrimitiveExpression(2)))), 59 new CodeMethodReturnStatement())); 60 CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression( 61 new CodeVariableReferenceExpression("i"), 62 CodeBinaryOperatorType.LessThan, 63 new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("ch"), "Length"), 64 CodeBinaryOperatorType.Subtract, new CodeArgumentReferenceExpression("start")), CodeBinaryOperatorType.Subtract, 65 new CodePrimitiveExpression(1)));//for 条件 66 CodeConditionStatement iterationBody = new CodeConditionStatement(new CodeBinaryOperatorExpression( 67 new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), 68 new CodeVariableReferenceExpression("i")), 69 CodeBinaryOperatorType.IdentityEquality, new CodeArrayIndexerExpression( 70 new CodeArgumentReferenceExpression("ch"), 71 new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), 72 CodeBinaryOperatorType.Add, new CodePrimitiveExpression(2)))), 73 new CodeAssignStatement( 74 new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), 75 new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, 76 new CodePrimitiveExpression(1))), 77 new CodePrimitiveExpression(‘+‘))); 78 iterationBody.FalseStatements.Add(new CodeAssignStatement( 79 new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), 80 new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, 81 new CodePrimitiveExpression(1))), 82 new CodePrimitiveExpression(‘-‘))); 83 CodeAssignStatement iteerationbody2 = new CodeAssignStatement(new CodeArrayIndexerExpression( 84 new CodeArgumentReferenceExpression("ch"), new CodeVariableReferenceExpression("i")), 85 new CodePrimitiveExpression(‘ ‘)); 86 fuHaoSanJiao.Statements.Add(new CodeIterationStatement(new CodeVariableDeclarationStatement(typeof(int), 87 "i", new CodeArgumentReferenceExpression("start")), condition, 88 new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodeBinaryOperatorExpression( 89 new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, 90 new CodePrimitiveExpression(2))), new CodeStatement[] { iterationBody, iteerationbody2 })); 91 // ch[ch.Length - start - 1] = ‘ ‘; 92 fuHaoSanJiao.Statements.Add(new CodeAssignStatement( 93 new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), 94 new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression( 95 new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("ch"), "Length"), 96 CodeBinaryOperatorType.Subtract, new CodeArgumentReferenceExpression("start")), 97 CodeBinaryOperatorType.Subtract, 98 new CodePrimitiveExpression(1))), new CodePrimitiveExpression(‘ ‘))); 99 // FuHaoSanJiao(ch, start + 1); 100 fuHaoSanJiao.Statements.Add(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), 101 "FuHaoSanJiao", new CodeArgumentReferenceExpression("ch"), 102 new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("start"), 103 CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)))); 104 CodeTypeDeclaration codeDomDemo2 = new CodeTypeDeclaration("CodeDomDemo2"); 105 codeDomDemo2.Members.Add(test); 106 codeDomDemo2.Members.Add(fuHaoSanJiao); 107 codeDomDemo2.Attributes = MemberAttributes.Public; 108 codeDomDemo2.Comments.Add(new CodeCommentStatement("this code is from CodeDom!")); 109 //codeDomDemo2.Members.AddRange(); 110 CodeNamespace nspace = new CodeNamespace("CodeDomDemo2"); 111 nspace.Imports.Add(new CodeNamespaceImport("System")); 112 nspace.Types.Add(codeDomDemo2); 113 return nspace; 114 } 115 } 116 } 117 输出代码为 : 118 119 namespace CodeDomDemo2 120 { 121 using System; 122 // this code is from CodeDom! 123 public class CodeDomDemo2 124 { 125 public void Test() 126 { 127 char[] ch = "+ - + - + + - -".ToCharArray(); 128 this.FuHaoSanJiao(ch, 0); 129 System.Console.Read(); 130 } 131 private void FuHaoSanJiao(char[] ch, int start) 132 { 133 System.Console.WriteLine(new string(ch)); 134 if ((start 135 >= (ch.Length / 2))) 136 { 137 return; 138 } 139 for (int i = start; (i 140 < ((ch.Length - start) 141 - 1)); i = (i + 2)) 142 { 143 if ((ch[i] == ch[(i + 2)])) 144 { 145 ch[(i + 1)] = ‘+‘; 146 } 147 else 148 { 149 ch[(i + 1)] = ‘-‘; 150 } 151 ch[i] = ‘ ‘; 152 } 153 ch[((ch.Length - start) 154 - 1)] = ‘ ‘; 155 this.FuHaoSanJiao(ch, (start + 1)); 156 } 157 } 158 }
原文地址:https://www.cnblogs.com/weifeng123/p/11398413.html
时间: 2024-10-17 08:54:51