CodeDom生成类文件
仅供个人学习
需要先引入System.CodeDom nuget包
1 using CodeGenerate.Entities; 2 using System; 3 using System.CodeDom; 4 using System.CodeDom.Compiler; 5 using System.Collections.Generic; 6 using System.Reflection; 7 using System.Text; 8 9 namespace CodeGenerate 10 { 11 public class GenerateCS 12 { 13 /// <summary> 14 /// 生成的类里面只有字段 15 /// </summary> 16 /// <param name="namespaceName"></param> 17 /// <param name="className"></param> 18 /// <param name="classType"></param> 19 /// <param name=""></param> 20 public static void FieldGenerate(string namespaceName, string className, TypeAttributes classType, string FileName, List<ClassFieldInfo> classInfos) 21 { 22 CodeCompileUnit unit = new CodeCompileUnit(); 23 24 //准备必要的命名空间(这个是指要生成的类的空间) 25 CodeNamespace sampleNamespace = new CodeNamespace(); 26 27 //导入必要的命名空间 28 //sampleNamespace.Imports.Add(new CodeNamespaceImport("System")); 29 30 //设置命名空间的名称 31 sampleNamespace.Name = namespaceName; 32 33 //准备要生成的类的定义 34 CodeTypeDeclaration Customerclass = new CodeTypeDeclaration(className); 35 36 //指定这是一个Class 37 Customerclass.IsClass = true; 38 Customerclass.TypeAttributes = classType; 39 //Customerclass.Attributes = MemberAttributes.Public | MemberAttributes.Const; 40 41 //把这个类放在这个命名空间下 42 sampleNamespace.Types.Add(Customerclass); 43 44 //把该命名空间加入到编译器单元的命名空间集合中 45 unit.Namespaces.Add(sampleNamespace); 46 47 //这是输出文件名称 48 string outputFile = FileName; 49 50 //添加字段 51 foreach (ClassFieldInfo item in classInfos) 52 { 53 CodeMemberField field = new CodeMemberField(item.FieldType, item.FiledName); 54 field.Attributes = item.FiledMemberAttributes; 55 field.Comments.Add(new CodeCommentStatement(item.FieldComments)); 56 //field.InitExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.Int64"), "12 * 1024 * 1024"); 57 field.InitExpression = new CodeFieldReferenceExpression(null, item.FileValue); 58 Customerclass.Members.Add(field); 59 } 60 61 //生成代码 62 CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); 63 CodeGeneratorOptions options = new CodeGeneratorOptions(); 64 options.BracingStyle = "C"; 65 options.BlankLinesBetweenMembers = true; 66 using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFile)) 67 { 68 provider.GenerateCodeFromCompileUnit(unit, sw, options); 69 } 70 } 71 72 public static void PropertiesGenerate(string namespaceName, string className, TypeAttributes classType, string FileName, List<ClassPropertyInfo> classInfos) 73 { 74 CodeCompileUnit unit = new CodeCompileUnit(); 75 76 //准备必要的命名空间(这个是指要生成的类的空间) 77 CodeNamespace sampleNamespace = new CodeNamespace(); 78 79 //导入必要的命名空间 80 //sampleNamespace.Imports.Add(new CodeNamespaceImport("System")); 81 82 //设置命名空间的名称 83 sampleNamespace.Name = namespaceName; 84 85 //准备要生成的类的定义 86 CodeTypeDeclaration Customerclass = new CodeTypeDeclaration(className); 87 88 //指定这是一个Class 89 Customerclass.IsClass = true; 90 Customerclass.TypeAttributes = classType; 91 //Customerclass.Attributes = MemberAttributes.Public | MemberAttributes.Const; 92 93 //把这个类放在这个命名空间下 94 sampleNamespace.Types.Add(Customerclass); 95 96 //把该命名空间加入到编译器单元的命名空间集合中 97 unit.Namespaces.Add(sampleNamespace); 98 99 //这是输出文件名称 100 string outputFile = FileName; 101 102 foreach(ClassPropertyInfo item in classInfos) 103 { 104 CodeMemberProperty property = new CodeMemberProperty(); 105 property.Attributes = item.PropertyMemberAttributes; 106 property.Name = item.PropertyName; 107 property.HasGet = item.IsHaveGet; 108 property.HasSet = item.IsHaveSet; 109 property.Type = new CodeTypeReference(item.PropertyType); 110 property.Comments.Add(new CodeCommentStatement(item.PropertyComments)); 111 //property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id"))); 112 //property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id"), new CodePropertySetValueReferenceExpression())); 113 Customerclass.Members.Add(property); 114 } 115 116 //生成代码 117 CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); 118 CodeGeneratorOptions options = new CodeGeneratorOptions(); 119 options.BracingStyle = "C"; 120 options.BlankLinesBetweenMembers = true; 121 using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFile)) 122 { 123 provider.GenerateCodeFromCompileUnit(unit, sw, options); 124 } 125 } 126 } 127 }
static void Generate(Dictionary<string, string> dic) { CodeCompileUnit unit = new CodeCompileUnit(); //准备必要的命名空间(这个是指要生成的类的空间) CodeNamespace sampleNamespace = new CodeNamespace(); //导入必要的命名空间 //sampleNamespace.Imports.Add(new CodeNamespaceImport("System")); //设置命名空间的名称 sampleNamespace.Name = "FiiiChain.Consensus"; //准备要生成的类的定义 CodeTypeDeclaration Customerclass = new CodeTypeDeclaration("BlockSetting"); //指定这是一个Class Customerclass.IsClass = true; Customerclass.TypeAttributes = TypeAttributes.Public; //Customerclass.Attributes = MemberAttributes.Public | MemberAttributes.Const; //把这个类放在这个命名空间下 sampleNamespace.Types.Add(Customerclass); //把该命名空间加入到编译器单元的命名空间集合中 unit.Namespaces.Add(sampleNamespace); //这是输出文件 string outputFile = "BlockSetting.cs"; //添加字段 foreach (KeyValuePair<string, string> item in dic) { CodeMemberField field = new CodeMemberField(typeof(long), item.Key); field.Attributes = MemberAttributes.Public | MemberAttributes.Const; //field.Comments.Add(new CodeCommentStatement("Max block size is 12 MB")); //field.InitExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.Int64"), "12 * 1024 * 1024"); field.InitExpression = new CodeFieldReferenceExpression(null, item.Value); Customerclass.Members.Add(field); } //添加属性 CodeMemberProperty property = new CodeMemberProperty(); property.Attributes = MemberAttributes.Public | MemberAttributes.Final; property.Name = "Id"; property.HasGet = true; property.HasSet = true; property.Type = new CodeTypeReference(typeof(System.String)); property.Comments.Add(new CodeCommentStatement("这是Id属性")); //property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id1"))); //property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id2"), new CodePropertySetValueReferenceExpression())); Customerclass.Members.Add(property); //添加方法(使用CodeMemberMethod)--此处略 //添加构造器(使用CodeConstructor) --此处略 //添加程序入口点(使用CodeEntryPointMethod) --此处略 //添加事件(使用CodeMemberEvent) --此处略 //添加特征(使用 CodeAttributeDeclaration) Customerclass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute)))); //生成代码 CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); CodeGeneratorOptions options = new CodeGeneratorOptions(); options.BracingStyle = "C"; options.BlankLinesBetweenMembers = true; using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFile)) { provider.GenerateCodeFromCompileUnit(unit, sw, options); } }