* U K I Y A H O N P O *
Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura,
che la diritta via era smarrita.
リロード   新規 下位ページ作成 編集 凍結 差分 添付 コピー 名前変更   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
浮子屋商店もよろしく。

C#コード断片/共通/C#のコードをメモリ内でコンパイルして実行

Top > C#コード断片 > 共通 > C#のコードをメモリ内でコンパイルして実行

C#のコードをメモリ内でコンパイルして実行

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
 
// ---- コンパイラ準備
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeCompiler cc = cscp.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.CompilerOptions="/r:System.dll;System.Windows.Forms.dll";
 
// ---- ソースコード
string txtsrc=
    "using System;"+
    "using System.Windows.Forms;"+
    "namespace Testnamespace{"+
        "public class TestClass{"+
            "public void Test(string s){"+
                "MeesageBox.Show(s);"+
            "}"+
        "}"+
    "}";
 // ---- コンパイル
CompilerResults cr = cc.CompileAssemblyFromSource(param, txtsrc);
foreach(CompilerError ce in cr.Errors){
    //エラー処理
}
 // ---- 結果アセンブリの、特定クラスの特定メソッドを実行
Assembly asm;
try{
    asm = cr.CompiledAssembly;
}catch(Exception ex){
    //エラー処理
}
Type type = asm.GetType("Testnamespace.TestClass");
object o=asm.CreateInstance("Testnamespace.TestClass");
MethodInfo mi = type.GetMethod("Test");
mi.Invoke(o, new object[]{"Hello,world"});

このページの最終更新日: 2006/03/02 11:19:11 JST (6624d)
このページのトラックバックURL: http://ukiya.sakura.ne.jp/index.php?tb_id=b7fb128729da26b25dfe2a790c755f26