C#在运行时动态创建类型的实现方法
本文实例讲述了C#在运行时动态创建类型的实现方法。是C#项目开发中很实用的技巧。分享给大家供大家参考。具体分析如下:
具体来说,C# 在运行时动态的创建类型是通过动态生成C#源代码,然后通过编译器编译成程序集的方式实现动态创建类型的。
主要功能代码如下:
public static Assembly NewAssembly()
{
//创建编译器实例。
provider = new CSharpCodeProvider();
//设置编译参数。
cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
// Generate an executable instead of
// a class library.
//cp.GenerateExecutable = true;
// Set the assembly file name to generate.
cp.OutputAssembly = "c:\1.dll";
// Generate debug information.
cp.IncludeDebugInformation = true;
// Save the assembly as a physical file.
cp.GenerateInMemory = false;
// Set the level at which the compiler
// should start displaying warnings.
cp.WarningLevel = 3;
// Set whether to treat all warnings as errors.
cp.TreatWarningsAsErrors = false;
// Set compiler argument to optimize output.
cp.CompilerOptions = "/optimize";
cp.ReferencedAssemblies.Add("System.dll");
//cp.ReferencedAssemblies.Add("System.Core.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
//cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
cp.ReferencedAssemblies.Add("System.Deployment.dll");
cp.ReferencedAssemblies.Add("System.Design.dll");
cp.ReferencedAssemblies.Add("System.Drawing.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
//创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("using System;using System.Windows.Forms;npublic class DynamicClass: UserControl n");
classSource.Append("{n");
classSource.Append("public DynamicClass()n{nInitializeComponent();nConsole.WriteLine("hello");}n");
classSource.Append( "private System.ComponentModel.IContainer components = null;nprotected override void Dispose(bool disposing)n{n");
classSource.Append( "if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);n}n");
classSource.Append( "private void InitializeComponent(){nthis.SuspendLayout();this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);");
classSource.Append( "this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Name = "DynamicClass";this.Size = new System.Drawing.Size(112, 74);this.ResumeLayout(false);n}");
//创建属性。
/*************************在这里改成需要的属性******************************/
classSource.Append(propertyString("aaa"));
classSource.Append(propertyString("bbb"));
classSource.Append(propertyString("ccc"));
classSource.Append("}");
System.Diagnostics.Debug.WriteLine(classSource.ToString());
//编译代码。
CompilerResults result = provider.CompileAssemblyFromSource(cp, classSource.ToString());
if (result.Errors.Count > 0)
{
for( int i = 0; i < result.Errors.Count; i ++)
Console.WriteLine(result.Errors[ i]);
Console.WriteLine("error");
return null;
}
//获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;
return assembly;
}
private static string propertyString(string propertyName)
{
StringBuilder sbProperty = new StringBuilder();
sbProperty.Append(" private int _" + propertyName + " = 0;n");
sbProperty.Append(" public int " + "" + propertyName + "n");
sbProperty.Append(" {n");
sbProperty.Append(" get{ return _" + propertyName + ";} n");
sbProperty.Append(" set{ _" + propertyName + " = value; }n");
sbProperty.Append(" }");
return sbProperty.ToString();
}
希望本文所述对大家的C#程序设计有所帮助
同类资源
- 动态菜单制作简洁快捷启动
易语言动态菜单制作简洁快捷启动例子源代码,自己用的快捷启动,灵感来自系统的开始菜单。...
- 随机打乱任何类型数组,1W个成员的数组也秒完成转换
易语言随机打乱任何类型数组,1W个成员的数组也秒完成转换例子源代码,自己有打乱文本数组的需求,搜了一下,都没...
- c#调用dll,dll加载epk
易语言c#调用dll,dll加载epk例子源代码,通过c#调用dll,易语言写dll调用epk。...
- 易语言汇编内核sockt、send、recv+动态创建线程
易语言汇编内核sockt、send、recv+动态创建线程例子源代码,此方法非常直接的防止了HOOK与抓包。...
- 动态/内存调用加载任意32/64模块源码汇编进程读写
易语言动态/内存调用加载任意32/64模块源码汇编进程读写例子源代码,只是适用于64位的部分函数,模块中以相同...
- DX11窗口创建模块
易语言DX11窗口创建模块例子源代码,沐咿呀窗口创建。...
- 软件加一个动态秘钥
软件加一个动态秘钥例子源代码,自行添加客户和服务端组件联网获取动态秘钥。...
- C#版的模块中所有的api和相关的结构体的声明
C#版的模块中所有的api和相关的结构体的声明例子源代码,直接用易语言封装成dll调用。...
- eWOW64Ext v1.21加载任意32/64模块,动态调用64位汇编进程读写
eWOW64Extv1.21加载任意32/64模块,动态调用64位汇编进程读写例子源代码,没有任何实质性内容更新,仅内部架构...
- mysql简单创建表增删改查
mysql简单创建表增删改查例子源代码,简单例子适合新手。...
- 4399桌面版,自定义动态菜单
读取一个xml作为菜单数据来刷新菜单,4399桌面版,自定义动态菜单例子源代码。...
- 动态图片生成工具
易语言动态图片生成工具例子源代码,自己尝试做了一下。...