Object reference not set to an instance of an object

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am using CodeDomProvider class to dynamically compile C# code. Usiung the following code it works fairly well:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; void Compile()
{
<span style="color:Blue; string[] code = <span style="color:Blue; new <span style="color:Blue; string[]
{
<span style="color:#A31515; @"using System;" +
<span style="color:#A31515; "using System.IO;" +
<span style="color:#A31515; "using System.Windows.Forms;" +
<span style="color:#A31515; "using System.Threading;" +
<span style="color:#A31515; "using System.Collections.Generic;" +
<span style="color:#A31515; "using System.Reflection;" +
<span style="color:#A31515; "using Atomic;" +

<span style="color:#A31515; "namespace Scripting" +
<span style="color:#A31515; "{" +
<span style="color:#A31515; "public class ScriptClass" +
<span style="color:#A31515; "{" +
ScriptF.CompilerSource() +
<span style="color:#A31515; "}" +
<span style="color:#A31515; "}"
};
CompilerParameters parameters = <span style="color:Blue; new CompilerParameters(<span style="color:Blue; new[]{ Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase)});

parameters.ReferencedAssemblies.Add(<span style="color:#A31515; "System.dll");
parameters.ReferencedAssemblies.Add(<span style="color:#A31515; "System.Windows.Forms.dll");
parameters.GenerateExecutable = <span style="color:Blue; true;
parameters.IncludeDebugInformation = <span style="color:Blue; false;
parameters.WarningLevel = 3;
parameters.TreatWarningsAsErrors = <span style="color:Blue; false;
parameters.CompilerOptions = <span style="color:#A31515; "/optimize";

CompilerResults results = CodeProvider.CompileAssemblyFromSource(parameters, code);

<span style="color:Blue; if (results.Errors.Count < 1)
results.CompiledAssembly.EntryPoint.Invoke(<span style="color:Blue; null, <span style="color:Blue; null);
<span style="color:Blue; else
<span style="color:Blue; foreach (CompilerError CompErr <span style="color:Blue; in results.Errors)
{
<span style="color:Blue; string output = <span style="color:Blue; string.Format(<span style="color:#A31515; "Line number {0}, Error number {1}, Error text: n{2}", CompErr.Line, CompErr.ErrorNumber, CompErr.ErrorText);
ScriptF.CompilerOutput(output);
}

}
[/code]


However I would like this to be done in memory and not generate an executable, but when I set

<div style="color:Black;background-color:White; <pre>
GenerateExecutable = <span style="color:Blue; false;<br/>
[/code]

I get the error "Object reference not set to an instance of an object" on the following line:


<div style="color:Black;background-color:White; <pre>
results.CompiledAssembly.EntryPoint.Invoke(<span style="color:Blue; null, <span style="color:Blue; null);
[/code]

Any ideas how to fix this?

View the full article
 
Back
Top