Skip to main content
 首页 » 编程设计

c#之CSharpCodeProvider 'cannot find metadata file' 使用 Mono 编译插件代码

2024年02月24日19zhengyun_ustc

我在应用程序运行时加载和编译的 XML 文件中有一些代码。

这在 Windows 上工作正常,但在 Mono 下我得到程序集引用错误。这是有问题的检查代码:

public static bool CompileSpell(Spell spell) 
{ 
            CSharpCodeProvider prov = new CSharpCodeProvider(); 
            CompilerParameters cp = new CompilerParameters(); 
            cp.GenerateExecutable = true; 
            cp.GenerateInMemory = true; 
            cp.ReferencedAssemblies.Add("system.dll"); 
            cp.ReferencedAssemblies.Add("system.xml.dll"); 
            cp.ReferencedAssemblies.Add("BasternaeMud.dll"); 
            cp.ReferencedAssemblies.Add("ZoneData.dll"); 
 
            Log.Trace("Compiling spell '" + spell.Name + "'."); 
 
            StringBuilder sb = new StringBuilder(); 
            int idx = spell.FileName.IndexOf('.'); 
            string file = spell.FileName; 
            if (idx > 0) 
            { 
                file = spell.FileName.Substring(0, idx); 
            } 
            int lines = GenerateWithMain(sb, spell.Code, "BasternaeMud"); 
 
            CompilerResults cr = prov.CompileAssemblyFromSource(cp,sb.ToString()); 
            .... 

我在编译结果中得到的具体错误是:

cannot find metadata file 'system.dll' at line 0 column 0. 
cannot find metadata file 'system.xml.dll' at line 0 column 0. 

Mono 显然不喜欢我在为 system.xml 和 system.xml.dll 编译的代码中添加引用程序集的方式。其他两个程序集添加得很好,这并不奇怪,因为它们是编译器实际执行的代码,并且存在于可执行目录中。

知道我需要做什么来解决这个问题吗?

也许我可以将那些 DLL 放在可执行目录中,但这感觉像是一个愚蠢的想法。

请您参考如下方法:

我不知道你是否在 Linux 上运行,但你可能想尝试正确地将 dll 名称大写,因为 Linux 区分大小写:

System.dll 
System.Xml.dll