C 動態生成類 列舉等

2022-06-09 08:03:12 字數 3430 閱讀 2429

private void generatecode() {

/*注意,先導入下面的命名空間

using system.codedom

using system.codedom.compiler;

using microsoft.csharp;

using system.reflection;

//準備乙個**編譯器單元

codecompileunit unit = new codecompileunit();

//準備必要的命名空間(這個是指要生成的類的空間)

codenamespace samplenamespace=new codenamespace("xizhang.com");

//匯入必要的命名空間

samplenamespace.imports.add(new codenamespaceimport("system"));

//準備要生成的類的定義

codetypedeclaration customerclass = new codetypedeclaration("customer");

//指定這是乙個class

customerclass.isclass = true;

customerclass.typeattributes = typeattributes.public | typeattributes.sealed;

//把這個類放在這個命名空間下

samplenamespace.types.add(customerclass);

//把該命名空間加入到編譯器單元的命名空間集合中

unit.namespaces.add(samplenamespace);

//這是輸出檔案

string outputfile = "customer.cs";

//新增字段

codememberfield field = new codememberfield(typeof(system.string), "_id");

field.attributes = memberattributes.private;

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(), "_id")));

property.setstatements.add(new codeassignstatement(new codefieldreferenceexpression(new codethisreferenceexpression(), "_id"), new codepropertysetvaluereferenceexpression()));

customerclass.members.add(property);

//新增方法(使用codemembermethod)--此處略

//新增構造器(使用codeconstructor) --此處略

//新增程式入口點(使用codeentrypointmethod) --此處略

//新增事件(使用codememberevent) --此處略

//新增特徵(使用 codeattributedeclaration)

customerclass.customattributes.add(new codeattributedeclaration(new codetypereference(typeof(serializableattribute))));

/* 生成列舉、列舉特性description

//生成特性

customerclass.customattributes.add(new codeattributedeclaration(new codetypereference(typeof(descriptionattribute)), new codeattributeargument(new codeprimitiveexpression(listitem.description))));

//指定這是乙個enum

customerclass.isenum = true;

customerclass.typeattributes = typeattributes.public;

//新增字段

codememberfield field = new codememberfield(typeof(enum), "列舉項1" + "=" + 1);

field.attributes = memberattributes.public;

//生成特性

field.customattributes.add(new codeattributedeclaration(new codetypereference(typeof(descriptionattribute)), new codeattributeargument(new codeprimitiveexpression("列舉描述資訊"))));

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);

最後生成的結果是

注:本文摘自   有改動

動態生成CS檔案(動態生成C 類)

1.codetypedeclaration 可用於表示宣告類 結構 介面或列舉的 官方傳送門 2.codememberfield 表示某種型別的字段的宣告 官方傳送門 3.codedomprovider 可用於建立和檢索 生成器和 編譯器的例項。生成器可用於以特定的語言生成 而 編譯器可用於將 編譯...

C 之列舉類

1.列舉類定義 語法形式 enum class 列舉型別名 底層型別 如果不指定底層型別,預設為int 例 enum class type enum class type char enum class category 2.列舉類的優勢 2.1 強作用域,其作用域限制在列舉類中 例 使用type的...

C 之列舉型別與列舉類

enumtype.cpp 定義控制台應用程式的入口點。include stdafx.h include using namespace std 定義乙個列舉型別,可以通過這個定義相應的列舉成員的識別符號 並用其中的乙個為他們賦值 定義在main函式體外屬於全域性變數 enum gameresult ...