Script on C#
CodeDOM を使えば、簡単にスクリプトを作れるような希ガス。
using System.IO; using Microsoft.CSharp; using System.CodeDom.Compiler; using System.Reflection; namespace ScriptOnCSharp { static class Program { [STAThread] static void Main() { // スクリプトの読み込み string script = File.ReadAllText("script.txt", Encoding.GetEncoding("shift-jis")); // スクリプトを C# のソースに変換 string source = ScriptToCSharp(script); // C# コンパイラ生成 CodeDomProvider cdp = new CSharpCodeProvider(); // コンパイル時のパラメータ生成 CompilerParameters cp = new CompilerParameters(); // 実行ファイルを生成する cp.GenerateExecutable = true; // コンパイルオプションに参照設定追加 cp.CompilerOptions = cp.CompilerOptions + " /r:System.Drawing.dll"; // コンパイル CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source); // アセンブリ情報からエントリーポイントを取得して実行 Assembly asm = cr.CompiledAssembly; MethodInfo mi = asm.EntryPoint; mi.Invoke(null, null); } static string ScriptToCSharp(string script) { // ここでスクリプトを C# のソースに変換する string source = @" using System; using System.Drawing; using System.Text; using System.IO; static class Program{ [STAThread] static void Main() { " + script + @" } } "; return source; } } }
// script.txt
File.WriteAllText( "result.txt" , "起動成功" );
これは script.txt をそのまま C# コンパイラに渡しているだけだけど、実際はここをごにょごにょ変換して、うまいぐあいにスクリプトっぽくなるように作る。
拡張性を考えるなら、CSharp とかの名前でブロックを作って、その中のブロックは C# そのままの構文で作れるようにすると良いかも。