Question

My solution structure in vs is as follows.

  • MyRuleEngineFramework
  • RulesEditor

MyRuleEngineFramework is a class library. RulesEditor is a winforms project. The latter references the former, however, that assembly is not consumed inside of the editor project anywhere.

There are just few lines of code where I do a kind of referencing:

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("MyRuleEngineFramework.dll");
cp.OutputAssembly = "temp.dll";
cp.GenerateInMemory = false;
CompilerResults cr = provider.CompileAssemblyFromSource(cp, SourceCode);

The above code executed fine and I got the compiled assembly the first time.

Then I registered MyRuleEngineFramework.dll to the gac.

After this the generated code (SourceCode) did not compile. Then I removed the reference to the dll in the editor project (while assembly was still in Gac), it still did not compile.

Now why does this happen?

Was it helpful?

Solution

Compilers require a reference assembly. Which should never be stored in the GAC. The GAC is only there to help the CLR find the proper version of whatever reference assembly you used at compile time. Compilers therefore never look in the GAC for a reference assembly. Nor can they possibly do so, they don't know what version to look for.

So this went wrong when you added the assembly to the GAC. The build system now no longer copies the DLL to the output directory since it can tell that this is no longer necessary. So your CodeDom code can no longer work since the reference assembly is missing and the compiler can no longer find it.

Don't use the GAC on your dev machine. Only, if ever, on the machine on which you install your program. In your particular case that should be never since you rely on the assembly you compile with to be identical to the assembly that gets loaded when you run the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top