CSharpCodeProvider: Why is a result of compilation out of context when debugging
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?
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)
精彩评论