CSharpCodeProvider 'cannot find metadata file' Compiling Plugin Code With Mono

Posted by Jason Champion on Stack Overflow See other posts from Stack Overflow or by Jason Champion
Published on 2010-03-24T07:42:30Z Indexed on 2010/03/24 7:43 UTC
Read the original article Hit count: 530

Filed under:
|
|
|

I have some code in an XML file that I load and compile at runtime in an application.

This works fine on Windows, but under Mono I get assembly reference errors. Here's the examine code in question:

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());
            ....

The specific errors I get in the compiler results are:

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

Mono obviously doesn't like the way I add referenced assemblies to the code I'm compiling for system.xml and system.xml.dll. The other two assemblies add fine, which is no surprise because they're the code that the compiler is actually executing from and exist in the executable directory.

Any clue what I need to do to fix this?

Maybe I could just drop those DLLs in the executable directory, but that feels like a dumb idea.

© Stack Overflow or respective owner

Related posts about c#

Related posts about csharpcodeprovider