該動(dòng)態(tài)編譯可以生成一個(gè)DTO類
1.創(chuàng)建一個(gè)控制臺(tái)項(xiàng)目
2.新建一個(gè)c#類,一個(gè)Dto類
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IList<ParameterDto> parameters = new List<ParameterDto>();
parameters.Add(new ParameterDto(1, "強(qiáng)制條件", "#MandatoryConditions", 4));
parameters.Add(new ParameterDto(2, "請(qǐng)假天數(shù)", "#LeaveDays", 1));
parameters.Add(new ParameterDto(3, "請(qǐng)假原因", "#LeaveReason", 2));
parameters.Add(new ParameterDto(4, "返校日期", "#RetuenDate", 3));
parameters.Add(new ParameterDto(5, "審核簽字", "#SignName", 2));
int result = CodeDomParameter(parameters);
}
//CodeDOM 動(dòng)態(tài)編譯實(shí)體類
//CodeDOM 動(dòng)態(tài)編譯實(shí)體類
public static int CodeDomParameter(IList<WParameterDto> parameters)
{
int result = 0;
//準(zhǔn)備一個(gè)代碼編譯器單元
CodeCompileUnit unit = new CodeCompileUnit();
//準(zhǔn)備必要的命名空間(這個(gè)是指要生成的類的空間)
CodeNamespace sampleNamespace = new CodeNamespace("WFlow.DTOs");
//導(dǎo)入必要的命名空間
sampleNamespace.Imports.Add(new CodeNamespaceImport("System"));
//準(zhǔn)備要生成的類的定義
CodeTypeDeclaration Customerclass = new CodeTypeDeclaration("InputParameterDto");
//指定這是一個(gè)Class
Customerclass.IsClass = true;
Customerclass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
//把這個(gè)類放在這個(gè)命名空間下
sampleNamespace.Types.Add(Customerclass);
//把該命名空間加入到編譯器單元的命名空間集合中
unit.Namespaces.Add(sampleNamespace);
//這是輸出文件
string outputFile = "D:\\Vs\\Workflow\\Workflow\\DTOs\\InputParameterDto.cs";
//參數(shù)數(shù)據(jù)類型滥比,1 - 數(shù)值型 2 - 字符串類型 3 - 時(shí)間日期 4 - 布爾型
CodeMemberField field = null;
foreach (WParameterDto parameter in parameters)
{
//添加字段
string paremeterCode ="_"+ parameter.parameterCode.Substring(1);
//賦值的字段
string parameterCodeP = parameter.parameterCode.Substring(1);
switch (parameter.parameterType)
{
case 1:
field = new CodeMemberField(typeof(int), paremeterCode);
break;
case 2:
field = new CodeMemberField(typeof(string), paremeterCode);
break;
case 3:
field = new CodeMemberField(typeof(string), paremeterCode);
break;
case 4:
field = new CodeMemberField(typeof(bool), paremeterCode);
break;
default:
break;
}
field.Attributes = MemberAttributes.Private;
field.Comments.Add(new CodeCommentStatement(parameter.parameterName));
Customerclass.Members.Add(field);
//添加屬性
CodeMemberProperty property = new CodeMemberProperty();
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
property.Name = parameterCodeP;
property.HasGet = true;
property.HasSet = true;
property.Type = null;
//參數(shù)數(shù)據(jù)類型九府,1 - 數(shù)值型 2 - 字符串類型 3 - 時(shí)間日期 4 - 布爾型
switch (parameter.parameterType)
{
case 1:
property.Type = new CodeTypeReference(typeof(int));
break;
case 2:
property.Type = new CodeTypeReference(typeof(string));
break;
case 3:
property.Type = new CodeTypeReference(typeof(string));
break;
case 4:
property.Type = new CodeTypeReference(typeof(bool));
break;
default:
break;
}
property.Comments.Add(new CodeCommentStatement(parameter.parameterName));
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), paremeterCode)));
property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), paremeterCode), new CodePropertySetValueReferenceExpression()));
Customerclass.Members.Add(property);
}
//添加方法(使用CodeMemberMethod)--此處略
//添加構(gòu)造器(使用CodeConstructor) --此處略
//添加程序入口點(diǎn)(使用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))
{
//if (File.Exists(@outputFile)) {
// File.Delete(@outputFile);
//}
provider.GenerateCodeFromCompileUnit(unit, sw, options);
result = 1;
}
//string sourceFile = @"InputParameterDto.cs";
//string destinationFile = @"../../DTOs/InputParameterDto.cs";
//FileInfo file = new FileInfo(sourceFile);
//if (file.Exists)
//{
// // true is overwrite
// file.CopyTo(destinationFile, true);
//}
return result;
}
}
}
namespace ConsoleApplication1
{
internal class ParameterDto
{
private int parameterId;
private string parameterName;
private string parameterCode;
private int parameterType;
public int ParameterId
{
get
{
return parameterId;
}
set
{
parameterId = value;
}
}
public string ParameterName
{
get
{
return parameterName;
}
set
{
parameterName = value;
}
}
public string ParameterCode
{
get
{
return parameterCode;
}
set
{
parameterCode = value;
}
}
public int ParameterType
{
get
{
return parameterType;
}
set
{
parameterType = value;
}
}
public ParameterDto(int parameterId, string parameterName, string parameterCode, int parameterType)
{
this.ParameterId = parameterId;
this.ParameterName = parameterName;
this.ParameterCode = parameterCode;
this.ParameterType = parameterType;
}
}
}
3.運(yùn)行控制臺(tái)項(xiàng)目
運(yùn)行后得到一個(gè)類甩栈,該類的位置默認(rèn)位于該項(xiàng)目文件夾中
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成臊旭。
// 運(yùn)行時(shí)版本:4.0.30319.42000
//
// 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果
// 重新生成代碼撒顿,這些更改將會(huì)丟失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace Workflow.DTOs
{
using System;
public class ParameterDto
{
// 強(qiáng)制條件
private bool MandatoryConditions;
// 請(qǐng)假天數(shù)
private int LeaveDays;
// 請(qǐng)假原因
private string LeaveReason;
// 返校日期
private string RetuenDate;
// 審核簽字
private string SignName;
public bool MandatoryConditions
{
get
{
return this.MandatoryConditions;
}
set
{
this.MandatoryConditions = value;
}
}
public int LeaveDays
{
get
{
return this.LeaveDays;
}
set
{
this.LeaveDays = value;
}
}
public string LeaveReason
{
get
{
return this.LeaveReason;
}
set
{
this.LeaveReason = value;
}
}
public string RetuenDate
{
get
{
return this.RetuenDate;
}
set
{
this.RetuenDate = value;
}
}
public string SignName
{
get
{
return this.SignName;
}
set
{
this.SignName = value;
}
}
}
}
4.生成dll
打開(kāi)項(xiàng)目荚板,選擇屬性,ctrl+s保存設(shè)置凤壁。
2.點(diǎn)擊生成,選擇重新生成解決方案
3.dll文件位于