Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用CompileAseMblyFromSource加载自定义程序集_C# - Fatal编程技术网

C# 使用CompileAseMblyFromSource加载自定义程序集

C# 使用CompileAseMblyFromSource加载自定义程序集,c#,C#,我不太确定该怎么办。总体目标是能够获取用户脚本,并在.NET环境中执行它。如果我不尝试加载自己的程序集,我已经编写了大部分代码,并且可以正常工作。但是,为了安全地让用户访问系统的内部部分,已经创建了一个代理DLL。这就是问题所在 现在这个代理DLL有一个接口 CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = tr

我不太确定该怎么办。总体目标是能够获取用户脚本,并在.NET环境中执行它。如果我不尝试加载自己的程序集,我已经编写了大部分代码,并且可以正常工作。但是,为了安全地让用户访问系统的内部部分,已经创建了一个代理DLL。这就是问题所在

现在这个代理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;
运行上述代码时,会引发以下错误:

System.IO.FileNotFoundException:无法加载文件或程序集 'file:///C:\用户…\AppData\Local\Temp\scts5w5o.dll'或其 依赖关系。系统找不到指定的文件

当然,我的第一个想法是,“…什么是scts5w5o.dll?”

这是
ScriptProxy.dll
未正确加载,还是ScriptProxy.dll本身正在尝试加载临时文件中的依赖项?还是完全不同


我应该提到,我是从NUnit测试运行程序执行这段代码的。我不确定这是否有区别。

这是因为编译步骤失败,您没有检查错误

    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;
    }

我不认为标记为
answer
的帖子真的是答案。。。然而我找到了答案

parameters.ReferencedAssembly.Add(typeof().Assembly.Location);

这意味着,如果您试图添加属于第三方的
dll
引用(有时.net dll也会给出例外),只需将其复制到
可执行文件夹中即可。。它会很好用的。。否则,您也可以在那里定义完整路径。

我将把它标记为答案,因为尽管它没有解决问题,但它确实帮助我获得了真正的错误消息,几乎同样有用!
parameters.ReferencedAssemblies.Add(typeof(<TYPE FROM DOMAIN.DLL>).Assembly.Location);