加载由罗斯林编译器生成的汇编 [英] Loading an assembly generated by the Roslyn compiler

查看:181
本文介绍了加载由罗斯林编译器生成的汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是罗斯林编译器生成Greeter.dll。我的问题时尝试加载DLL文件。

I'm generating a Greeter.dll using the Roslyn compiler. My problem occurs trying to load the DLL file.

下面的代码:

using System;

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

using System.IO;
using System.Reflection;
using System.Linq;

namespace LoadingAClass
{
    class Program
    {
        static void Main(string[] args)
        {
            var syntaxTree = SyntaxTree.ParseCompilationUnit(@"
class Greeter
{
    static void Greet()
    {
        Console.WriteLine(""Hello, World"");
    }
}");

            var compilation = Compilation.Create("Greeter.dll",
                syntaxTrees: new[] { syntaxTree },
                references: new[] {
                    new AssemblyFileReference(typeof(object).Assembly.Location),
                    new AssemblyFileReference(typeof(Enumerable).Assembly.Location),
                });

            Assembly assembly;
            using (var file = new FileStream("Greeter.dll", FileMode.Create))
            {
                EmitResult result = compilation.Emit(file);
            }

            assembly = Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(), @"Greeter.dll"));
            Type type = assembly.GetType("Greeter");
            var obj = Activator.CreateInstance(type);

            type.InvokeMember("Greet",
                BindingFlags.Default | BindingFlags.InvokeMethod,
                null,
                obj,
                null);

            Console.WriteLine("<ENTER> to continue");
            Console.ReadLine();

        }
    }
    // Thanks, http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx
}

上线时,会出现错误信息 =装配Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(),@Greeter.dll)); 和读取

The error message occurs on the line assembly = Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(), @"Greeter.dll")); and reads

林MODUL wurde EIN Assemblymanifest erwartet。 (Ausnahme冯HRESULT:0x80131018)

Im Modul wurde ein Assemblymanifest erwartet. (Ausnahme von HRESULT: 0x80131018)

大致翻译为

这是集清单预计在模块中。

An assembly manifest was expected in the module.

有谁知道我在这里失踪?

Does anyone know what I'm missing here?

推荐答案

我已经加入罗斯林支持的 O2 Plarform ,这里是如何使用它的罗斯林支持编译(代码),创造(和装配),并调用(及其方法)的一行代码:

I have been adding Roslyn support to the O2 Plarform and here is how you can use its Roslyn support to compile (code), create (and assembly) and invoke (its method) one line of code:

return @"using System; class Greeter { static string Greet() {  return ""Another hello!!""; }}"
        .tree().compiler("Great").create_Assembly().type("Greeter").invokeStatic("Greet"); 

//O2Ref:O2_FluentSharp_Roslyn.dll

下面是一个版本一个执行代码片段看起来像你(我加了一个返回值):

Here is a version that executes a code snippet that looks like yours (I added a return value):

panel.clear().add_ConsoleOut();
var code = @"
using System;
class Greeter
{
    static string Greet()
    { 
        Console.WriteLine(""Hello, World""); 
        return ""hello from here"";
    }
}";
var tree = code.astTree();
if (tree.hasErrors())
    return tree.errors();   

var compiler = tree.compiler("Great")
                   .add_Reference("mscorlib");

if (compiler.hasErrors()) 
    return compiler.errors();    

var assembly  =tree.compiler("Great")
                   .create_Assembly();

return assembly.type("Greeter")
               .invokeStatic("Greet"); 

//O2Ref:O2_FluentSharp_Roslyn.dll
//O2File:_Extra_methods_Roslyn_API.cs
//O2File:API_ConsoleOut.cs

有关一对夫妇更多的细节和这是什么样子的截图,看到这个博客帖子:的 1号线进行编译,创建和执行:O2脚本使用罗斯林以动态编译和执行的方法

For a couple more details and screenshots of what this looks like, see this blog post: 1 line to compile, create and execute: O2 Script to use Roslyn to Dynamically compile and execute a method

更新:看的 http://blog.diniscruz.com/search/label/Roslyn 的罗斯林相关文章和工具(使用O2平台创建)的大量数字

UPDATE: see http://blog.diniscruz.com/search/label/Roslyn for a large number number of Roslyn related posts and tools (created using the O2 Platform)

这篇关于加载由罗斯林编译器生成的汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆