201

我想知道是否可以将 C# 代码片段保存到文本文件(或任何输入流)中,然后动态执行它们?假设提供给我的内容可以在任何 Main() 块中正常编译,是否可以编译和/或执行此代码?出于性能原因,我更愿意编译它。

至少,我可以定义一个他们需要实现的接口,然后他们会提供一个实现该接口的代码“部分”。

4

7 回答 7

194

C#/所有静态 .NET 语言中的最佳解决方案是将CodeDOM用于此类事情。(请注意,它的另一个主要目的是动态构建代码位,甚至是整个类。)

这是来自LukeH 的博客的一个很好的简短示例,它使用一些 LINQ 也只是为了好玩。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

class Program
{
    static void Main(string[] args)
    {
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
        parameters.GenerateExecutable = true;
        CompilerResults results = csc.CompileAssemblyFromSource(parameters,
        @"using System.Linq;
            class Program {
              public static void Main(string[] args) {
                var q = from i in Enumerable.Range(1,100)
                          where i % 2 == 0
                          select i;
              }
            }");
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
    }
}

这里最重要的类是CSharpCodeProvider利用编译器动态编译代码的类。如果你想然后运行代码,你只需要使用一点反射来动态加载程序集并执行它。

是 C# 中的另一个示例(虽然稍微不那么简洁),它还准确地向您展示了如何使用System.Reflection命名空间运行运行时编译的代码。

于 2009-05-05T19:03:40.173 回答
81

您可以将一段 C# 代码编译到内存中,并使用 Roslyn 生成汇编字节。已经提到过,但值得在这里添加一些 Roslyn 示例。以下是完整的示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;

namespace RoslynCompileSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // define source code, then parse it (to the type used for compilation)
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
                using System;

                namespace RoslynCompileSample
                {
                    public class Writer
                    {
                        public void Write(string message)
                        {
                            Console.WriteLine(message);
                        }
                    }
                }");

            // define other necessary objects for compilation
            string assemblyName = Path.GetRandomFileName();
            MetadataReference[] references = new MetadataReference[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)
            };

            // analyse and generate IL code from syntax tree
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                // write IL code into memory
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    // handle exceptions
                    IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic => 
                        diagnostic.IsWarningAsError || 
                        diagnostic.Severity == DiagnosticSeverity.Error);

                    foreach (Diagnostic diagnostic in failures)
                    {
                        Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
                    }
                }
                else
                {
                    // load this 'virtual' DLL so that we can use
                    ms.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(ms.ToArray());

                    // create instance of the desired class and call the desired function
                    Type type = assembly.GetType("RoslynCompileSample.Writer");
                    object obj = Activator.CreateInstance(type);
                    type.InvokeMember("Write",
                        BindingFlags.Default | BindingFlags.InvokeMethod,
                        null,
                        obj,
                        new object[] { "Hello World" });
                }
            }

            Console.ReadLine();
        }
    }
}
于 2015-04-02T15:43:51.413 回答
42

其他人已经就如何在运行时生成代码给出了很好的答案,所以我想我会解决你的第二段。我对此有一些经验,只想分享我从那次经历中学到的教训。

至少,我可以定义一个他们需要实现的接口,然后他们会提供一个实现该接口的代码“部分”。

如果您使用 aninterface作为基本类型,您可能会遇到问题。interface如果您在未来添加一个新方法,那么所有现有的客户端提供的实现interfacenow 的类都将变为抽象,这意味着您将无法在运行时编译或实例化客户端提供的类。

在发布旧界面大约 1 年并分发大量需要支持的“遗留”数据之后,当需要添加新方法时,我遇到了这个问题。我最终创建了一个继承自旧接口的新接口,但这种方法使得加载和实例化客户端提供的类变得更加困难,因为我必须检查哪个接口可用。

我当时想到的一种解决方案是使用实际的类作为基类型,如下所示。类本身可以标记为抽象,但所有方法都应该是空的虚拟方法(不是抽象方法)。然后,客户可以覆盖他们想要的方法,我可以向基类添加新方法,而不会使现有的客户提供的代码无效。

public abstract class BaseClass
{
    public virtual void Foo1() { }
    public virtual bool Foo2() { return false; }
    ...
}

不管这个问题是否适用,您都应该考虑如何对代码库和客户端提供的代码之间的接口进行版本控制。

于 2009-05-05T19:47:45.123 回答
12

发现这很有用 - 确保编译的程序集引用您当前引用的所有内容,因为您很有可能希望您正在编译的 C# 在发出此代码的代码中使用某些类等:

(字符串code是正在编译的动态 C#)

        var refs = AppDomain.CurrentDomain.GetAssemblies();
        var refFiles = refs.Where(a => !a.IsDynamic).Select(a => a.Location).ToArray();
        var cSharp = (new Microsoft.CSharp.CSharpCodeProvider()).CreateCompiler();
        var compileParams = new System.CodeDom.Compiler.CompilerParameters(refFiles);
        compileParams.GenerateInMemory = true;
        compileParams.GenerateExecutable = false;

        var compilerResult = cSharp.CompileAssemblyFromSource(compileParams, code);
        var asm = compilerResult.CompiledAssembly;

在我的例子中,我发出了一个类,它的名称存储在一个字符串中className,它有一个名为 的公共静态方法Get(),返回类型为StoryDataIds。下面是调用该方法的样子:

        var tempType = asm.GetType(className);
        var ids = (StoryDataIds)tempType.GetMethod("Get").Invoke(null, null);

警告:编译可能会非常缓慢,非常缓慢。在我们相对较快的服务器上,一小段相对简单的 10 行代码在 2-10 秒内以正常优先级编译。您永远不应该将调用CompileAssemblyFromSource()与具有正常性能预期的任何事物联系起来,例如 Web 请求。相反,在低优先级线程上主动编译您需要的代码,并有办法处理需要该代码准备好的代码,直到它有机会完成编译。例如,您可以在批处理作业过程中使用它。

于 2017-12-09T19:00:14.193 回答
4

我最近需要为单元测试生成流程。这篇文章很有用,因为我创建了一个简单的类来使用作为字符串的代码或我的项目中的代码来做到这一点。要构建此类,您需要ICSharpCode.DecompilerMicrosoft.CodeAnalysisNuGet 包。这是课程:

using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

public static class CSharpRunner
{
   public static object Run(string snippet, IEnumerable<Assembly> references, string typeName, string methodName, params object[] args) =>
      Invoke(Compile(Parse(snippet), references), typeName, methodName, args);

   public static object Run(MethodInfo methodInfo, params object[] args)
   {
      var refs = methodInfo.DeclaringType.Assembly.GetReferencedAssemblies().Select(n => Assembly.Load(n));
      return Invoke(Compile(Decompile(methodInfo), refs), methodInfo.DeclaringType.FullName, methodInfo.Name, args);
   }

   private static Assembly Compile(SyntaxTree syntaxTree, IEnumerable<Assembly> references = null)
   {
      if (references is null) references = new[] { typeof(object).Assembly, typeof(Enumerable).Assembly };
      var mrefs = references.Select(a => MetadataReference.CreateFromFile(a.Location));
      var compilation = CSharpCompilation.Create(Path.GetRandomFileName(), new[] { syntaxTree }, mrefs, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

      using (var ms = new MemoryStream())
      {
         var result = compilation.Emit(ms);
         if (result.Success)
         {
            ms.Seek(0, SeekOrigin.Begin);
            return Assembly.Load(ms.ToArray());
         }
         else
         {
            throw new InvalidOperationException(string.Join("\n", result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error).Select(d => $"{d.Id}: {d.GetMessage()}")));
         }
      }
   }

   private static SyntaxTree Decompile(MethodInfo methodInfo)
   {
      var decompiler = new CSharpDecompiler(methodInfo.DeclaringType.Assembly.Location, new DecompilerSettings());
      var typeInfo = decompiler.TypeSystem.MainModule.Compilation.FindType(methodInfo.DeclaringType).GetDefinition();
      return Parse(decompiler.DecompileTypeAsString(typeInfo.FullTypeName));
   }

   private static object Invoke(Assembly assembly, string typeName, string methodName, object[] args)
   {
      var type = assembly.GetType(typeName);
      var obj = Activator.CreateInstance(type);
      return type.InvokeMember(methodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, args);
   }

   private static SyntaxTree Parse(string snippet) => CSharpSyntaxTree.ParseText(snippet);
}

要使用它,请调用以下Run方法:

void Demo1()
{
   const string code = @"
   public class Runner
   {
      public void Run() { System.IO.File.AppendAllText(@""C:\Temp\NUnitTest.txt"", System.DateTime.Now.ToString(""o"") + ""\n""); }
   }";

   CSharpRunner.Run(code, null, "Runner", "Run");
}

void Demo2()
{
   CSharpRunner.Run(typeof(Runner).GetMethod("Run"));
}

public class Runner
{
   public void Run() { System.IO.File.AppendAllText(@"C:\Temp\NUnitTest.txt", System.DateTime.Now.ToString("o") + "\n"); }
}
于 2019-06-24T19:06:58.327 回答
3

要编译,您只需启动对 csc 编译器的 shell 调用。试图保持路径和开关笔直,您可能会头疼,但它肯定可以做到。

C# 角壳示例

编辑:或者更好的是,按照诺多林的建议使用 CodeDOM...

于 2009-05-05T18:59:55.970 回答
1
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Reflection;

namespace ASL
{
    class Program
    {
        [Obsolete]
        static void Main(string[] args)
        {
            string code = @"
                using System;

             namespace First
             {
                public class Program
                {
                  public static void Main()
                    {
                        " +
                        "Console.WriteLine(\"Hello, world!\");"
                        + @"
                    }
                }
             }";
            Console.WriteLine(code);
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            // Reference to System.Drawing library
            parameters.ReferencedAssemblies.Add("System.Drawing.dll");
            // True - memory generation, false - external file generation
            parameters.GenerateInMemory = true;
            // True - exe file generation, false - dll file generation
            parameters.GenerateExecutable = true;
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
            if (results.Errors.HasErrors)
            {
                StringBuilder sb = new StringBuilder();

                foreach (CompilerError error in results.Errors)
                {
                    sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
                }

                throw new InvalidOperationException(sb.ToString());
            }
            Assembly assembly = results.CompiledAssembly;
            Type program = assembly.GetType("First.Program");
            MethodInfo main = program.GetMethod("Main");
            main.Invoke(null, null);
            Console.ReadLine();
        }
    }
}
于 2021-07-28T12:16:39.807 回答