// ソースコードの雛形
static string cs = @"
using System.IO;
using System.Text;
public class CSHello {
public static void kenmomen(string[] filenames)
{
コピペゾーン♪
}
}";
// プラグイン呼び出し用のメソッドオブジェクト
private MethodInfo miKenmomen;
private void kenmomen(string[] filenames)
{
// リフレクションでプラグイン実行
this.miKenmomen.Invoke(null, new object[] { filenames });
}
private void Form1_Load(object sender, EventArgs e)
{
CSharpCodeProvider cscp = new CSharpCodeProvider();
ICodeCompiler cc = cscp.CreateCompiler();
// コンパイルパラメータ(メモリ上にコンパイル、Exeではない。)
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.GenerateExecutable = false;
// プラグインソースコードを取得
// ここの文字コードはSJISかUTF8か悩みどころ
string plugin = File.ReadAllText("kenmomen.cs", Encoding.UTF8);
// ビルドに必要なDLLを追加
// これも動的に出来ないと詰む可能性あり。
// csファイルに参照DLLセクションを設けるなどの対応が必要。
param.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
param.ReferencedAssemblies.Add("System.dll");
param.ReferencedAssemblies.Add("System.Core.dll");
param.ReferencedAssemblies.Add("System.Data.dll");
param.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
param.ReferencedAssemblies.Add("System.IO.dll");
param.ReferencedAssemblies.Add("System.Xml.dll");
param.ReferencedAssemblies.Add("System.Xml.Linq.dll");
// ソースコード雛形にプラグインをマージしてコンパイル
CompilerResults cr = cc.CompileAssemblyFromSource(param, cs.Replace("コピペゾーン♪", plugin));
// コンパイルされた型とメソッドを取り出す。
Type type = cr.CompiledAssembly.GetType("CSHello");
this.miKenmomen = type.GetMethod("kenmomen");
}
ICAgICAgICAvLyDjgr3jg7zjgrnjgrPjg7zjg4njga7pm5vlvaIKICAgICAgICBzdGF0aWMgc3RyaW5nIGNzID0gQCIKICAgICAgICAgICAgICB1c2luZyBTeXN0ZW0uSU87CiAgICAgICAgICAgICAgdXNpbmcgU3lzdGVtLlRleHQ7CiAgICAgICAgICAgICAgcHVibGljIGNsYXNzIENTSGV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zaW9ucy5kbGwiKTsKICAgICAgICAgICAgcGFyYW0uUmVmZXJlbmNlZEFzc2VtYmxpZXMuQWRkKCJTeXN0ZW0uSU8uZGxsIik7CiAgICAgICAgICAgIHBhcmFtLlJlZmVyZW5jZWRBc3NlbWJsaWVzLkFkZCgiU3lzdGVtLlhtbC5kbGwiKTsKICAgICAgICAgICAgcGFyYW0uUmVmZXJlbmNlZEFzc2VtYmxpZXMuQWRkKCJTeXN0ZW0uWG1sLkxpbnEuZGxsIik7CgogICAgICAgICAgICAvLyDjgr3jg7zjgrnjgrPjg7zjg4npm5vlvaLjgavjg5fjg6njgrDjgqTjg7PjgpLjg57jg7zjgrjjgZfjgabjgrPjg7Pjg5HjgqTjg6sKICAgICAgICAgICAgQ29tcGlsZXJSZXN1bHRzIGNyID0gY2MuQ29tcGlsZUFzc2VtYmx5RnJvbVNvdXJjZShwYXJhbSwgY3MuUmVwbGFjZSgi44Kz44OU44Oa44K+44O844Oz4pmqIiwgcGx1Z2luKSk7CgogICAgICAgICAgICAvLyDjgrPjg7Pjg5HjgqTjg6vjgZXjgozjgZ/lnovjgajjg6Hjgr3jg4Pjg4njgpLlj5bjgorlh7rjgZnjgIIKICAgICAgICAgICAgVHlwZSB0eXBlID0gY3IuQ29tcGlsZWRBc3NlbWJseS5HZXRUeXBlKCJDU0hlbGxvIik7CiAgICAgICAgICAgIHRoaXMubWlLZW5tb21lbiA9IHR5cGUuR2V0TWV0aG9kKCJrZW5tb21lbiIpOwogICAgICAgIH0K
prog.cs(13,15): error CS1525: Unexpected symbol `string', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
prog.cs(24,16): error CS1525: Unexpected symbol `MethodInfo', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
prog.cs(26,16): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
prog.cs(26,37): warning CS0658: `]' is invalid attribute target. All attributes in this attribute section will be ignored
prog.cs(65,1): error CS8025: Parsing error
Compilation failed: 4 error(s), 1 warnings