標籤:osc 建立案頭捷徑 line stringbu window dede mem span border
本文執行個體講述了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#程式設計有所協助
除聲明外,
跑步客文章均為原創,轉載請以連結形式標明本文地址
C#在運行時動態建立類型的實現方法
本文地址: http://www.paobuke.com/develop/c-develop/pbk23524.html
相關內容C#中增加SQLite事務操作支援與使用方法C#êμ??′????÷1|?üμ?ComboBoxC#實現建立案頭捷徑與添加網頁到收藏夾的樣本C#簡單訪問SQLite資料庫的方法(安裝,串連,查詢等)
C#資料結構之隊列(Quene)執行個體詳解C#編程自學之資料類型和變數三C#中GridView動態添加列的實現方法C#實現利用反射簡化給類欄位賦值的方法
C#在運行時動態建立類型的實現方法