欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#在運行時動態(tài)創(chuàng)建類型的實現方法

 更新時間:2014年09月12日 10:46:58   投稿:shichen2014  
這篇文章主要介紹了C#在運行時動態(tài)創(chuàng)建類型的實現方法,主要通過動態(tài)生成C#代碼再編譯成程序集來實現動態(tài)創(chuàng)建類型的,需要的朋友可以參考下

本文實例講述了C#在運行時動態(tài)創(chuàng)建類型的實現方法。是C#項目開發(fā)中很實用的技巧。分享給大家供大家參考。具體分析如下:

具體來說,C# 在運行時動態(tài)的創(chuàng)建類型是通過動態(tài)生成C#源代碼,然后通過編譯器編譯成程序集的方式實現動態(tài)創(chuàng)建類型的

主要功能代碼如下:

public static Assembly NewAssembly()
{
  //創(chuàng)建編譯器實例。  
  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");

  //創(chuà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();
}

希望本文所述對大家的C#程序設計有所幫助

相關文章

  • C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    這篇文章主要介紹了C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別的相關知識點內容,有需要朋友們參考下。
    2019-08-08
  • C#實現的文件批量重命名功能示例

    C#實現的文件批量重命名功能示例

    這篇文章主要介紹了C#實現的文件批量重命名功能,結合具體實例形式分析了C#針對文件的遍歷、屬性修改相關操作技巧,需要的朋友可以參考下
    2017-07-07
  • C#實現的4種常用數據校驗方法小結(CRC校驗,LRC校驗,BCC校驗,累加和校驗)

    C#實現的4種常用數據校驗方法小結(CRC校驗,LRC校驗,BCC校驗,累加和校驗)

    本文主要介紹了C#實現的4種常用數據校驗方法小結(CRC校驗,LRC校驗,BCC校驗,累加和校驗),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C#10的13個特性

    C#10的13個特性

    本文詳細講解了C#10的13個特性,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • C#繪制中國國旗的方法

    C#繪制中國國旗的方法

    這篇文章主要介紹了C#繪制中國國旗的方法,以實例形式較為詳細的分析了C#圖形繪制的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#利用反射實現多數據庫訪問

    C#利用反射實現多數據庫訪問

    本文詳細講解了C#利用反射實現多數據庫訪問的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#實現漢字轉區(qū)位碼的示例代碼

    C#實現漢字轉區(qū)位碼的示例代碼

    區(qū)位碼是一個4位的十進制數,每個區(qū)位碼都對應著一個唯一的漢字,區(qū)位碼的前兩位叫做區(qū)碼,后兩位叫做位碼,下面我們就來看看如何使用C#實現漢字轉區(qū)位碼吧
    2024-01-01
  • C#使用Ado.net讀取Excel表的方法

    C#使用Ado.net讀取Excel表的方法

    這篇文章主要介紹了C#使用Ado.net讀取Excel表的方法,涉及C#使用OLEDB操作Excel的方法,需要的朋友可以參考下
    2015-04-04
  • C#判斷獲取的是文件夾還是文件的實例

    C#判斷獲取的是文件夾還是文件的實例

    今天小編就為大家分享一篇C#判斷獲取的是文件夾還是文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • C# Soap調用WebService的實例

    C# Soap調用WebService的實例

    下面小編就為大家?guī)硪黄狢# Soap調WebService的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12

最新評論