【问题标题】:How to get Assemblies Generated at Runtime to use Project Dependencies? C#如何在运行时生成程序集以使用项目依赖项? C#
【发布时间】:2017-10-03 08:13:33
【问题描述】:

是否可以允许动态生成的程序集访问生成新程序集的项目中存在的依赖项?我正在使用 Unity 和 C#,我添加了一个程序集,其中包含它现在所属的项目中存在的依赖项,但我收到此错误:FileNotFoundException: 无法加载文件或程序集“ModAssembly000.dll”或其依赖项之一.我收到此错误是因为我尝试将“使用 UnityEngine”放在脚本的顶部。这是项目中已经存在的获取新程序集并调用方法的代码:

        CompilerParameters parameters = new CompilerParameters();
    parameters.GenerateExecutable = false;
    parameters.GenerateInMemory = true;
    parameters.OutputAssembly = generatedName;
    CompilerResults r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromFile(parameters, filePath);
    r.CompiledAssembly.GetType("ModData").GetMethod("Run").Invoke(null, BindingFlags.Static, null, null, null);

这是动态创建的程序集的来源:

using UnityEngine;

public class ModData {
    public static string modName = "Super kool mod";
    public static string modVersion = "1.2.1";

    public static void Run() {
        Debug.Log("it worked :D");
    }
}

UnityEngine(即 Debug.Log)存在于生成此程序集的代码中。有没有办法让新创建的程序集使用它上面存在的 UnityEngine,这样我就可以允许新代码在上面的项目中做任何事情?我知道 ModAssembly000.dll 存在,因为如果我删除“使用 UnityEngine”行,那么我可以毫无问题地访问动态程序集的静态字符串字段。

【问题讨论】:

    标签: c# .net .net-assembly dynamically-generated


    【解决方案1】:

    我的猜测是你需要将 Unity 添加到引用的程序集列表中:

    parameters.ReferencedAssemblies.Add("UnityEngine.dll");
    

    【讨论】:

    • 确实是这样。我忽略了它。感谢您的宝贵时间!
    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2010-09-23
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多