简体   繁体   English

使用CompileAssemblyFromSource加载自定义程序集

[英]Loading Custom Assemblies with CompileAssemblyFromSource

I'm not quite sure where to go with this. 我不太清楚该怎么做。 The overall objective is to be able to take a user script, and execute it within a .NET environment. 总体目标是能够获取用户脚本,并在.NET环境中执行它。 I have most of the code written and things work provided I don't attempt to load my own assemblies. 我已经编写了大部分代码并且工作正常,我不会尝试加载自己的程序集。 However, to safely give users access to internal portions of the system, a proxy DLL has been created. 但是,为了安全地让用户访问系统的内部部分,已经创建了代理DLL。 This is where the problem seems to be. 这就是问题所在。

Right now this proxy DLL has one thing in it, an interface. 现在这个代理DLL有一个东西,一个接口。

CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("ScriptProxy.dll");

Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerResults result = provider.CompileAssemblyFromSource(options, script);

// This line here throws the error:
return result.CompiledAssembly;

Running the above code, it throws the following error: 运行上面的代码,它会抛出以下错误:

System.IO.FileNotFoundException : Could not load file or assembly 'file:///C:\\Users...\\AppData\\Local\\Temp\\scts5w5o.dll' or one of its dependencies. System.IO.FileNotFoundException:无法加载文件或程序集'file:/// C:\\ Users ... \\ AppData \\ Local \\ Temp \\ scts5w5o.dll'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

Of course my first thought is, "...and what is scts5w5o.dll?" 当然我的第一个想法是,“......什么是scts5w5o.dll?”

Is this the ScriptProxy.dll not loading properly, or is the ScriptProxy.dll itself trying to load up dependencies, which are in a temp file somewhere? 这是ScriptProxy.dll无法正确加载,还是ScriptProxy.dll本身试图加载依赖项,这些依赖项位于某个临时文件中? Or is it something completely different? 或者它是完全不同的东西?

I should mention, I'm executing this code from the NUnit test runner. 我应该提一下,我正在从NUnit测试运行器执行此代码。 I'm not sure if that makes a difference. 我不确定这是否有所作为。

This is because the compilation step failed and you have not check it for errors... 这是因为编译步骤失败,您没有检查错误...

    static Assembly Compile(string script)
    {
        CompilerParameters options = new CompilerParameters();
        options.GenerateExecutable = false;
        options.GenerateInMemory = true;
        options.ReferencedAssemblies.Add("System.dll");
        options.ReferencedAssemblies.Add("ScriptProxy.dll");

        Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
        CompilerResults result = provider.CompileAssemblyFromSource(options, script);

        // Check the compiler results for errors
        StringWriter sw = new StringWriter();
        foreach (CompilerError ce in result.Errors)
        {
            if (ce.IsWarning) continue;
            sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
        }
        // If there were errors, raise an exception...
        string errorText = sw.ToString();
        if (errorText.Length > 0)
            throw new ApplicationException(errorText);

        return result.CompiledAssembly;
    }

I don't think the post marked as answer is really an answer !!! 我不认为标记为answer的帖子真的是答案 ... However i found the answer here ......但是我在这里找到了答案

parameters.ReferencedAssemblies.Add(typeof(<TYPE FROM DOMAIN.DLL>).Assembly.Location);

It mean if you trying to add dll reference that are third party(sometime .net dlls also giving exceptions) then simply copy it in executable folder .. it will work fine .. else you can also define full path there.. 这意味着如果你试图添加第三方的dll引用(有时.net dll也给出例外)然后只需将它复制到executable folder ..它会正常工作..否则你也可以在那里定义完整路径..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM