2

(I'm having trouble finding an appropriate title for this)

I am required to use a C# CodeDomProvider to compile several files containing a class each. I wrote a 'generator' class containing said provider (amongst other stuff) which is called to process each file. I can't past the whole code here, but the relevant parts would be :

    private Assembly CompileFile(string _file)
    {
        CodeDom provider = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters cparams = new CompilerParameters();
        cparams.GenerateExecutable = false;
        cparams.GenerateInMemory = true;
        cparams.ReferencedAssemblies.Add (/*assembly containing attributes classes */ assemblyReference);
        cparams.OutputAssembly =  "outputAssembly"; // Required to be able to compile more than one file with the same provider, for some reason
        CompilerResults results = provider.CompileAssemblyFromFile(_file);
        if (results.Errors.HasErrors)
        {
            StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
            foreach (CompilerError error in results.Errors )
            {
                errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
            }
            throw new Exception(errors.ToString());
        }

        return results.CompiledAssembly;
    }

And in another method :

Assembly asm = CompileFile(_file);
foreach (Type t in asm.GetTypes())
  Console.WriteLine (t.ToString ());

If this code is called twice within the same generator class, the asm.GetTypes() would return only the first type compiled, never the second. I also tried to re instantiate the provider and the compilation params with the same result. I also tried to reinstantiate the whole generator class, again, same problem. No exceptions are thrown during compilation.

What could I possibly do wrong ?

0 Answers0