出现“ Assembly.EntryPoint.Invoke”问题[C#] [英] Trouble with "Assembly.EntryPoint.Invoke" [C#]

查看:147
本文介绍了出现“ Assembly.EntryPoint.Invoke”问题[C#]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

我在运行时使用 Microsoft.CSharp中的 CSharpCodeProvider 编译C#代码。如果我双击生成的文件,则创建的应用程序可以完美运行。

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.

但是,如果我正在用 Assembly加载我创建的程序集。加载并使用 Assembly.Load( ...)。EntryPoint.Invoke(null,null)调用入口点方法,我得到 NullReferenceException



NullReferenceException 引用 .EntryPoint -Property的值。

在调试包含已加载的Assembly的变量时,VisualStudio显示以下内容:

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.


The NullReferenceException is referring to the value of the .EntryPoint-Property.
When I debug the variable containing the loaded Assembly, VisualStudio shows the following:




图片较大

错误在德语中,表示该指令指针中 local或参数 asm的值不可用,因此可以尚未确定。在优化过程中可能已将其删除。



此时,我想添加的是生成的程序集未优化(我还添加了编译器参数 / optimize-,这会阻止优化。)


我通过尝试以下代码执行了另一项测试以确定错误源: / p>

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.


I want to add at this point that my generated assembly is not optimized (I also added the compiler argument /optimize-, which prevents optimization.)
I performed an other test to determine the error source by trying this Code:

Assembly asm = Assembly.LoadFile(Assembly.GetExecutingAssembly().Location);

asm.EntryPoint.Invoke(null, argv);

此代码还在包含<$的行中抛出 NullReferenceException c $ c>调用通话。



这里有人知道这个错误是从哪里来的,我可以解决这个问题吗?


非常感谢:)






编辑:

This code is also throwing a NullReferenceException at the line containing the Invoke-call.


Does someone here know, where this error is coming from and I can solve it?
Thank you very much :)


入口点方法定义如下:

namespace tmp
{
   public static class Program
   {
      [STAThread]
      public static void Main(string[] argv)
      { ... }
   }
}

我也尝试用 .EntryPoint.Invoke(空,新字符串[] { test, argument}),但没有解决问题:/

I also tried to invoke it with .EntryPoint.Invoke(null, new string[]{"test", "argument"}), but it didn't solve the problem :/

编辑#2::我发现了我的错误-请查看来自 @的评论汉斯·帕桑(Hans Passant)和我自己的解决方案

EDIT #2: I found my error - please look at the comment from @Hans Passant and myself for the soulution

〜closed〜

推荐答案

复制并粘贴

// 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);

这篇关于出现“ Assembly.EntryPoint.Invoke”问题[C#]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆