2

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?

5
  • Are you debugging in release mode? This may happen to optimizations of unused variable. Commented Mar 19, 2010 at 19:24
  • No, I am not, it is in debug mode. Reason I am debugging is because next line is supposed to load assembly and use Activator.CreateInstance, but it cannot find it so it throws an error. Commented Mar 19, 2010 at 19:27
  • You wanna post your question as an answer, slightly modified. I had "Optimize Code" checked in project build properties. Your "optimizations of unused variable" gave an idea to check that. Commented Mar 19, 2010 at 19:31
  • @Elisha - this comment only exists (with the @prefix) to draw your attention to @epitka's comment ("post that as an answer"). Commented Mar 19, 2010 at 19:39
  • for info, if you want to make sure another user sees your reply to their comment, use the @prefix - then it shows in their messages area (only works for the first @whatever in your comment, though) Commented Mar 19, 2010 at 19:40

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.