仅供个人学习
需要先引入System.CodeDom nuget包
using CodeGenerate.Entities;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using System.Text; namespace CodeGenerate
{
public class GenerateCS
{
/// <summary>
/// 生成的类里面只有字段
/// </summary>
/// <param name="namespaceName"></param>
/// <param name="className"></param>
/// <param name="classType"></param>
/// <param name=""></param>
public static void FieldGenerate(string namespaceName, string className, TypeAttributes classType, string FileName, List<ClassFieldInfo> classInfos)
{
CodeCompileUnit unit = new CodeCompileUnit(); //准备必要的命名空间(这个是指要生成的类的空间)
CodeNamespace sampleNamespace = new CodeNamespace(); //导入必要的命名空间
//sampleNamespace.Imports.Add(new CodeNamespaceImport("System")); //设置命名空间的名称
sampleNamespace.Name = namespaceName; //准备要生成的类的定义
CodeTypeDeclaration Customerclass = new CodeTypeDeclaration(className); //指定这是一个Class
Customerclass.IsClass = true;
Customerclass.TypeAttributes = classType;
//Customerclass.Attributes = MemberAttributes.Public | MemberAttributes.Const; //把这个类放在这个命名空间下
sampleNamespace.Types.Add(Customerclass); //把该命名空间加入到编译器单元的命名空间集合中
unit.Namespaces.Add(sampleNamespace); //这是输出文件名称
string outputFile = FileName; //添加字段
foreach (ClassFieldInfo item in classInfos)
{
CodeMemberField field = new CodeMemberField(item.FieldType, item.FiledName);
field.Attributes = item.FiledMemberAttributes;
field.Comments.Add(new CodeCommentStatement(item.FieldComments));
//field.InitExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.Int64"), "12 * 1024 * 1024");
field.InitExpression = new CodeFieldReferenceExpression(null, item.FileValue);
Customerclass.Members.Add(field);
} //生成代码
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);
}
} public static void PropertiesGenerate(string namespaceName, string className, TypeAttributes classType, string FileName, List<ClassPropertyInfo> classInfos)
{
CodeCompileUnit unit = new CodeCompileUnit(); //准备必要的命名空间(这个是指要生成的类的空间)
CodeNamespace sampleNamespace = new CodeNamespace(); //导入必要的命名空间
//sampleNamespace.Imports.Add(new CodeNamespaceImport("System")); //设置命名空间的名称
sampleNamespace.Name = namespaceName; //准备要生成的类的定义
CodeTypeDeclaration Customerclass = new CodeTypeDeclaration(className); //指定这是一个Class
Customerclass.IsClass = true;
Customerclass.TypeAttributes = classType;
//Customerclass.Attributes = MemberAttributes.Public | MemberAttributes.Const; //把这个类放在这个命名空间下
sampleNamespace.Types.Add(Customerclass); //把该命名空间加入到编译器单元的命名空间集合中
unit.Namespaces.Add(sampleNamespace); //这是输出文件名称
string outputFile = FileName; foreach(ClassPropertyInfo item in classInfos)
{
CodeMemberProperty property = new CodeMemberProperty();
property.Attributes = item.PropertyMemberAttributes;
property.Name = item.PropertyName;
property.HasGet = item.IsHaveGet;
property.HasSet = item.IsHaveSet;
property.Type = new CodeTypeReference(item.PropertyType);
property.Comments.Add(new CodeCommentStatement(item.PropertyComments));
//property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id")));
//property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id"), new CodePropertySetValueReferenceExpression()));
Customerclass.Members.Add(property);
} //生成代码
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);
}
}
}
}
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);
}
}