c# - 如何将 Roslyn 脚本提交用作其他 Roslyn 编译中的程序集

标签 c# .net .net-assembly roslyn

我想在另一个非脚本 Roslyn 编译中将脚本作为动态程序集重用,但我终究无法弄清楚如何实现它。

例如,假设我以正常方式创建脚本,然后使用类似以下内容将脚本作为程序集发送到字节流:

var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = script.GetCompilation().WithOptions(compilationOptions);
using (var ms = new MemoryStream())
{
    EmitResult result = compilation.Emit(ms);
    ms.Seek(0, SeekOrigin.Begin);
    assembly = Assembly.Load(ms.ToArray());
}

现在,假设我想将该程序集提供给另一个非脚本编译作为引用。我不能只使用 assembly因为没有MetadataReference.CreateFrom...()方法支持传递实际的 Assembly实例。作为动态程序集,它没有位置,所以我不能使用 MetadataReference.CreateFromFile() .

过去我用过MetadataReference.CreateFromStream()对于这类成功的事情,但是当程序集代表脚本提交时这似乎不起作用(我不知道为什么)。编译继续进行,但是一旦您尝试使用提交中的类型,您就会收到如下错误:

System.InvalidCastException: [A]Foo cannot be cast to [B]Foo. Type A originates from 'R*19cecf20-a48e-4a31-9b65-4c0163eba857#1-0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array. Type B originates from 'R*19cecf20-a48e-4a31-9b65-4c0163eba857#1-0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.

我猜这与在评估与作为字节数组加载时处于不同上下文中的提交程序集有关。对于在以后的非脚本编译中使用脚本提交中定义的对象和方法的最佳方式,我希望有任何见解或指导。

7 月 29 日更新

我能够得到一个证明问题的最小重现。它可以在 https://github.com/daveaglick/ScriptingAssemblyReuse 找到.

在生成重现时,很明显这个问题的一个重要组成部分是脚本通过了 Type其中一个类输出到调用代码,调用代码然后使用该 Type实例化对象的实例,然后将该实例传递到引用脚本程序集的编译中。当从主机应用程序创建的类型的实例转换为引用编译中的类型时,会发生不匹配。当我重新阅读时,它听起来很困惑,所以希望下面的代码能让它更清楚。

这里是触发这个问题的所有代码:

namespace ScriptingAssemblyReuse
{
    public class Globals
    {
        public IFactory Factory { get; set; }    
    }

    public interface IFactory
    {
        object Get();
    }

    public class Factory<T> : IFactory where T : new()
    {
        public object Get() => new T();
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Run();
        }

        private Assembly _scriptAssembly;

        public void Run()
        {
            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

            // Create the script
            Script<object> script = CSharpScript.Create(@"
                public class Foo { }
                Factory = new ScriptingAssemblyReuse.Factory<Foo>();
                ", ScriptOptions.Default.WithReferences(MetadataReference.CreateFromFile(typeof(IFactory).Assembly.Location)), typeof(Globals));

            // Create a compilation and get the dynamic assembly
            CSharpCompilationOptions scriptCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            Compilation scriptCompilation = script.GetCompilation().WithOptions(scriptCompilationOptions);
            byte[] scriptAssemblyBytes;
            using (MemoryStream ms = new MemoryStream())
            {
                EmitResult result = scriptCompilation.Emit(ms);
                ms.Seek(0, SeekOrigin.Begin);
                scriptAssemblyBytes = ms.ToArray();
            }
            _scriptAssembly = Assembly.Load(scriptAssemblyBytes);

            // Evaluate the script
            Globals globals = new Globals();
            script.RunAsync(globals).Wait();

            // Create the consuming compilation
            string assemblyName = Path.GetRandomFileName();
            CSharpParseOptions parseOptions = new CSharpParseOptions();
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
                public class Bar
                {
                    public void Baz(object obj)
                    {
                        Script.Foo foo = (Script.Foo)obj;  // This is the line that triggers the exception 
                    }
                }", parseOptions, assemblyName);
            CSharpCompilationOptions compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            string assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
            CSharpCompilation compilation = CSharpCompilation.Create(assemblyName, new[] {syntaxTree},
                new[]
                {
                    MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")),
                    MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")),
                    MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")),
                    MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll"))
                }, compilationOptions);
            using (MemoryStream ms = new MemoryStream(scriptAssemblyBytes))
            {
                compilation = compilation.AddReferences(MetadataReference.CreateFromStream(ms));
            }

            // Get the consuming assembly
            Assembly assembly;
            using (MemoryStream ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] assemblyBytes = ms.ToArray();
                assembly = Assembly.Load(assemblyBytes);
            }

            // Call the consuming assembly
            Type barType = assembly.GetExportedTypes().First(t => t.Name.StartsWith("Bar", StringComparison.Ordinal));
            MethodInfo bazMethod = barType.GetMethod("Baz");
            object bar = Activator.CreateInstance(barType);
            object obj = globals.Factory.Get();
            bazMethod.Invoke(bar, new []{ obj });  // The exception bubbles up and gets thrown here
        }

        private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (_scriptAssembly != null && args.Name == _scriptAssembly.FullName)
            {
                // Return the dynamically compiled script assembly if given it's name
                return _scriptAssembly;
            }
            return null;
        }
    }
}

最佳答案

我想我已经解开了谜团。这是发生了什么:

  • 创建脚本。
  • 获取脚本的编译,但覆盖编译选项。具体来说,它使用默认的 ScriptClassName,即 Script 而不是脚本 API 生成的脚本(例如 Submission#0)- 这就是问题的关键
  • 使用覆盖的选项发出程序集并将其从流中加载到内存中。
  • 运行脚本。此时,使用字节数组将两个不同且不兼容的同名程序集加载到AppDomain中。
  • 将流作为元数据引用添加到新的编译中,并在代码中使用它。它编译得很好,因为它使用的是从重写选项生成的程序集。您将无法使用脚本创建的实际程序集对其进行编译,因为 Submission#0 之类的名称在 C# 中是非法的。如果不是,那么您可以将实际脚本 Assembly 实例放入全局变量中,并在 OnAssemblyResolve 中使用它。
  • 使用 Submission#0+Foo 类型的参数调用方法 Baz 并尝试将其转换为 Script+Foo

总而言之 - 我认为使用当前的 Roslyn 脚本 API 不可能做到这一点。然而,这些 API 并不是编译脚本的唯一方式。您可以自己创建一个编译并将 SourceCodeKind 设置为 Script。你必须自己做很多事情,比如运行主脚本方法、处理全局变量等。我做过这样的事情 in RoslynPad因为我希望脚本程序集与 PDB 一起加载(因此异常会有行信息)。

关于c# - 如何将 Roslyn 脚本提交用作其他 Roslyn 编译中的程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646619/

相关文章:

c# - 如何从 Azure Key Vault KeyBundle 创建 X509Certificate2 对象

c# - 形成sql以将bool返回给c#

c# - C# 中的线程安全队列 <t>

c# - 如何在 C++/Cli 中使用可等待对象

c# - 无法加载文件或程序集 'Oracle.DataAccess 错误

c# - 如何使用 IN sql 关键字构建参数化查询?

c# - 使用 Newtonsoft JSON 的 ObjectCreationHandling 的说明?

.net - 将工作单元和存储库模式与 Entity Framework 结合使用的好处

c# - .Net 4.6 网站未正确加载引用程序集

c# - MEF 插件依赖项未加载,因为未检查插件目录