C#実行時に動的にタイプを作成する実装方法

4326 ワード

この例では、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;
public class DynamicClass: UserControl
"); classSource.Append("{
"); classSource.Append("public DynamicClass()
{
InitializeComponent();
Console.WriteLine(\"hello\");}
"); classSource.Append( "private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
"); classSource.Append( "if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);
}
"); classSource.Append( "private void InitializeComponent(){
this.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);
}"); // 。 /************************* ******************************/ 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;
"); sbProperty.Append(" public int " + "" + propertyName + "
"); sbProperty.Append(" {
"); sbProperty.Append(" get{ return _" + propertyName + ";}
"); sbProperty.Append(" set{ _" + propertyName + " = value; }
"); sbProperty.Append(" }"); return sbProperty.ToString(); }

本稿で述べたC#プログラムの設計に役立つことを願っています.