문제

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?

도움이 되었습니까?

해결책

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");    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top