C#在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建類型的實(shí)現(xiàn)方法
本文實(shí)例講述了C#在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建類型的實(shí)現(xiàn)方法。是C#項(xiàng)目開發(fā)中很實(shí)用的技巧。分享給大家供大家參考。具體分析如下:
具體來說,C# 在運(yùn)行時(shí)動(dòng)態(tài)的創(chuàng)建類型是通過動(dòng)態(tài)生成C#源代碼,然后通過編譯器編譯成程序集的方式實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類型的。
主要功能代碼如下:
public static Assembly NewAssembly()
{
//創(chuàng)建編譯器實(shí)例。
provider = new CSharpCodeProvider();
//設(shè)置編譯參數(shù)。
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");
//創(chuàng)建動(dòng)態(tài)代碼。
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}");
//創(chuàng)建屬性。
/*************************在這里改成需要的屬性******************************/
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();
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助
相關(guān)文章
C#十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制int的方法
這篇文章主要介紹了C#十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制int的方法,涉及C#操作數(shù)制轉(zhuǎn)換的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
WPF仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View
這篇文章主要為大家詳細(xì)介紹了WPF如何模仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
DevExpress獲取TreeList可視區(qū)域節(jié)點(diǎn)集合的實(shí)現(xiàn)方法
這篇文章主要介紹了DevExpress獲取TreeList可視區(qū)域節(jié)點(diǎn)集合的實(shí)現(xiàn)方法,有一定實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)ListView選中項(xiàng)向上或向下移動(dòng)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ListView選中項(xiàng)向上或向下移動(dòng)的方法,通過兩個(gè)按鈕點(diǎn)擊事件實(shí)現(xiàn)ListView選中項(xiàng)的上下移動(dòng)功能,需要的朋友可以參考下2015-06-06
利用C#實(shí)現(xiàn)繪制出地球旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實(shí)現(xiàn)繪制出地球旋轉(zhuǎn)的效果,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下2023-02-02
基于C#實(shí)現(xiàn)的端口掃描器實(shí)例代碼
這篇文章主要介紹了基于C#實(shí)現(xiàn)的端口掃描器實(shí)例代碼,需要的朋友可以參考下2014-07-07
C#難點(diǎn)逐個(gè)擊破(4):main函數(shù)
貌似我是在寫C#的學(xué)習(xí)筆記哦,不過反正可以利用這個(gè)機(jī)會(huì)來好好溫習(xí)下基礎(chǔ)知識(shí),這其中很多知識(shí)點(diǎn)都屬于平時(shí)視而見的小知識(shí)2010-02-02
C#中哈希表(HashTable)用法實(shí)例詳解(添加/移除/判斷/遍歷/排序等)
這篇文章主要介紹了C#中哈希表(HashTable)用法,簡單講述了哈希表的原理并結(jié)合實(shí)例形式詳細(xì)分析了C#針對(duì)哈希表進(jìn)行添加、移除、判斷、遍歷、排序等操作的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06

