MethodInfo.Invoke 抛出 TargetInvocationException c#

MethodInfo.Invoke Throw TargetInvocationException c#

这是我的 TargetInvocationException 问题。

此错误发生在带有 MethodInfo method.Invoke(null, arguments);

的行上

仅供参考,我正在研究别人在 2014 年编写的代码,它应该可以工作,但事实并非如此。在整个周末搜索之后,我没有发现问题。 不要犹豫,向我询问有关我所拥有的代码的更多信息,也许问题来自其他地方。

在主程序中它看起来像这样(不是所有的代码,有些部分在这些行之间,但你不需要它们):

static void Main (string[] args)
{
    [... Code before]
    object[] arguments = { popup };
    MethodInfo method;
    CodeCompiler cc = new CodeCompiler();
    method = cc.CompileCode(fichier, "test", "RequestWeb", "requestW", true, arguments);

    List<Account> li = (List<Account>)method.Invoke(null, arguments); // TargetInvocationException Here is the error
}

这是 class 帐户:

public class Account
{
    virtual public string libelle { get; set; }
    virtual public List<AccountStat> listR { get; set; }

    public Account()
    {
        this.listR = new List<AccountStat>(); // This go to another class where List<AccountStat> is defined
    }
}

我试图通过 InnerException 系统了解谁告诉我:

"The index was off limits. It should not be negative and must be less than the size of the collection \ r \ nName parameter . StartIndex"

但是我还是不明白什么意思...是List<Account>的问题吗?

感谢您的帮助。

逐条搜索后,我发现错误在requestW class 错误所在的"The index was off limits. It should not be negative and must be less than the size of the collection \ r \ nName parameter . StartIndex" 处。 (在逐点检查所有内容后,似乎这个错误再次出现在我的 class 中,我正在尝试阅读 Internet 网站。)

在这个 class 中,我有一部分从 <tbody 的一侧到另一侧,分解后我看到 StartIndex 值不是正值而是负值,抛出异常。

所以问题不在 List<Account> li = (List<Account>)method.Invoke(null, arguments); 本身,而是在 method class 是我的 requestW 被调用并给出了异常。 谢谢大家帮我解决问题:)