* 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.
ホーム
一覧
検索
最終更新
リンク
ヘルプ
新規
リンク
手動リンク
自動相互リンク
駄でべWiki:FrontPage
ヘルプ
整形ルール
プラグインマニュアル
浮子屋商店もよろしく。
C#コード断片/共通/C#のコードをメモリ内でコンパイルして実行
をテンプレートにして作成
開始行:
* C#のコードをメモリ内でコンパイルして実行 [#b79abafc]
#code(Csharp,nooutline){{
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...
// ---- ソースコード
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, ...
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"});
}}
終了行:
* C#のコードをメモリ内でコンパイルして実行 [#b79abafc]
#code(Csharp,nooutline){{
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...
// ---- ソースコード
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, ...
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"});
}}
ページ名: