Pregunta

Using the code in this link I am attempting to compile a generated .cs file at runtime. The file (call it Gen.cs) has been generated by CodeDOM and subclasses another file (call it Base.cs). I get the following error message from the runtime compilation:

The type or namespace name `Base' could not be found. Are you missing a using directive or an assembly reference?

I know that this error means it can't find Base.cs when it's compiling. I'm new to both C# and compiling C# files dynamically, so this is a bit hard to pick apart. Neither Base nor Gen use namespaces, and both .cs files are in the same directory. Is there anything I'm missing? Why can't Gen.cs see Base.cs?

EDIT - This may or may not be relevant, but here's the result of a manual compilation via gmcs (I'm on MacOS)

mtrc$ gmcs -lib:Base.cs Gen.cs Gen.cs(13,27): error CS0246: The type or namespace name `Base' could not be found. Are you missing a using directive or an assembly reference?

¿Fue útil?

Solución

Typically a compiler will only compile source files that it's been told to compile in a single pass. ie: specify all .cs names on the command line, or specify to compile all .cs files in a given folder.

In this case:

string []arrCode = { code, "base.cs" };
CompilerResults results = 
   compiler.CompileAssemblyFromSource(compilerparams, arrCode);    

Alternative syntax:

CompilerResults results = 
   compiler.CompileAssemblyFromSource(compilerparams, code, "base.cs");    
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top