views:

89

answers:

1

I have following code snippet that i use to compile class at the run time.

//now compile the runner
var codeProvider = new CSharpCodeProvider(
  new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

string[] references = new string[]
  {
    "System.dll", "System.Core.dll", "System.Core.dll"
  };
CompilerParameters parameters = new CompilerParameters();

parameters.ReferencedAssemblies.AddRange(references);               
parameters.OutputAssembly = "CGRunner";
parameters.GenerateInMemory = true;
parameters.TreatWarningsAsErrors = true;

CompilerResults result = codeProvider.CompileAssemblyFromSource(parameters, template);

Whenever I step through the code to debug the unit test, and I try to see what is the value of "result" I get an error that name "result" does not exist in current context. Why?

+1  A: 

Are you debugging in release mode? This may happen to optimizations of unused variable.

For example:

public void OptimizedMethod()
{
    int x = 5; // In optimized mode it's not possible to watch the variable
}

Code optimization happens when running in release mode, or when setting "Optimize code" in project properties (under build tab)

Elisha