I have the following problem:
I am compiling C#-code at runtime using the CSharpCodeProvider in Microsoft.CSharp. The created application runs perfectly, if I double-click on the generated file.
If, however, I am loading my created assembly with Assembly.Load and invoking the entrypoint-method with Assembly.Load("...").EntryPoint.Invoke(null, null), I get a NullReferenceException.
.EntryPoint-Property.
Larger image
The error is in German and means The value of "local" or argument "asm" is not available in this instruction pointer and can therefore not be determined. It may have been removed during optimization.
/optimize-, which prevents optimization.)
Assembly asm = Assembly.LoadFile(Assembly.GetExecutingAssembly().Location);
asm.EntryPoint.Invoke(null, argv);
This code is also throwing a NullReferenceException at the line containing the Invoke-call.
The entrypoint-method is defined as follows:
namespace tmp
{
public static class Program
{
[STAThread]
public static void Main(string[] argv)
{ ... }
}
}
I also tried to invoke it with .EntryPoint.Invoke(null, new string[]{"test", "argument"}), but it didn't solve the problem :/
EDIT #2: I found my error - please look at the comment from @Hans Passant and myself for the soulution
~closed~
Copy and paste from Hans Passant's comment:
// Get your assembly.
Assembly asm = Assembly.LoadFile(Assembly.GetExecutingAssembly().Location);
// Get your point of entry.
MethodInfo entryPoint = asm.EntryPoint;
// Invoke point of entry with arguments.
entryPoint.Invoke(null, new object[] { new string[] { "arg1", "arg2", "etc" } } );
If you want to access an assembly from an embedded resource, use this snippet:
byte[] yourResource = Properties.Resources.YourResourceName;
Assembly asm = Assembly.Load(yourResource);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With