5

私はさまざまなアドホックなデータ変更や補助的なタスクを実行するために、Python を広範囲に使用してきました。私は C# を学んでいるので、これらのスクリプトのいくつかを C# で書き直すことができれば楽しいと思います。

.cs ファイルを取り、それを ala python で実行する実行可能ファイルはありますか?

4

3 に答える 3

4

このようなことができます (これと同様のコードでコンソール アプリを作成します)。

...
using System.Reflection;
using System.CodeDom.Compiler;
...

namespace YourNameSpace
{
  public interface IRunner
  {
    void Run();
  }

  public class Program
  {
    static Main(string[] args)
    {
      if(args.Length == 1)
      {
        Assembly compiledScript = CompileCode(args[0]);
        if(compiledScript != null)
          RunScript(compiledScript);
      }
    }

    private Assembly CompileCode(string code)
    {
      Microsoft.CSharp.CSharpCodeProvider csProvider = new 
Microsoft.CSharp.CSharpCodeProvider();

      CompilerParameters options = new CompilerParameters();
      options.GenerateExecutable = false;
      options.GenerateInMemory = true;

      // Add the namespaces needed for your code
      options.ReferencedAssemblies.Add("System");
      options.ReferencedAssemblies.Add("System.IO");
      options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

      // Compile the code
      CompilerResults result;
      result = csProvider.CompileAssemblyFromSource(options, code);

      if (result.Errors.HasErrors)
      {
        // TODO: Output the errors
        return null;
      }

      if (result.Errors.HasWarnings)
      {
        // TODO: output warnings
      }

      return result.CompiledAssembly;
    }

    private void RunScript(Assembly script)
    {
      foreach (Type type in script.GetExportedTypes())
      {
        foreach (Type iface in type.GetInterfaces())
        {
          if (iface == typeof(YourNameSpace.Runner))
          {
            ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
              if (constructor != null && constructor.IsPublic)
              {
                YourNameSpace.IRunner scriptObject = constructor.Invoke(null) as 
YourNameSpace.IRunner;

                if (scriptObject != null)
                {
                  scriptObject.Run();
                }
                else
                {
                  // TODO: Unable to create the object
                }
              }
              else
              {
                // TODO: Not implementing IRunner
              }
            }
          }
        }
      }
  }
}

このコンソール アプリを作成したら、コマンド プロンプトで次のように起動できます。

YourPath:\> YourAppName.exe "public class Test : IRunnder { public void Run() { 
Console.WriteLine("woot"); } }"

インライン コードではなくファイルを受け入れるように Main メソッドを簡単に変更できるため、コンソール アプリは Python または Ruby インタープリターと同様の動作をします。ファイル名をアプリケーションに渡し、メイン関数の StreamReader でそれを読み取り、内容を CompileCode メソッドに渡すだけです。このようなもの:

static void Main(string[] args)
{
  if(args.Length == 1 && File.Exists(args[0]))
  {
    var assambly = CompileCode(File.ReadAllText(args[0]));
    ...
  }  
}

そしてコマンドラインで:

YourPath:\> YourApp.exe c:\script.cs

IRunner インターフェイスを実装する必要があります。インターフェイスを継承せずに、ハードコードされた Start メソッドを呼び出すだけでもかまいません。これは、クラスをオンザフライでコンパイルして実行するという概念を示すためのものです。

それが役立つことを願っています。

于 2011-06-02T10:53:15.477 に答える
1

CS-Script: C# スクリプト エンジン

http://www.csscript.net/

于 2011-05-18T07:45:38.543 に答える