简体   繁体   English

MethodInfo.Invoke抛出TargetInvocationException C#

[英]MethodInfo.Invoke Throw TargetInvocationException c#

So here is my problem with a TargetInvocationException. 所以这是我与TargetInvocationException有关的问题。

This error occurs on a line with a MethodInfo method.Invoke(null, arguments); 在使用MethodInfo method.Invoke(null, arguments);的行上发生此错误MethodInfo method.Invoke(null, arguments);

FYI I'm working on a code someone made before me, in 2014, it was supposed to work but it's not. 仅供参考,我正在编写某人在我之前编写的代码,2014年应该可以,但事实并非如此。 After searching during the whole week end, I didn't find the problem. 在整个周末搜索之后,我没有发现问题。 Don't hesitate to ask me for more informations about the code I have, maybe the problem comes from somewhere else. 不要犹豫,询问我有关我的代码的更多信息,也许问题出在其他地方。

In the main program it's look like this ( there is not all the code, some parts are between those lines, but you don't need them ): 在主程序中,它看起来像这样(不是所有代码,某些部分在这些行之间,但是您不需要它们):

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
}

And here is the class Account : 这是Account类:

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
    }
}

I tried to understand with the InnerException system who tell me : 我试图通过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" “索引超出限制。它不能为负,并且必须小于collection \\ r \\ nName参数的大小。StartIndex”

but I still don't understand what it mean... Is the problem with List<Account> ? 但我仍然不明白这是什么意思... List<Account>是否有问题?

Thank you for your help. 谢谢您的帮助。

After searching point by point everything, I found that the error was in the requestW class where the "The index was off limits. It should not be negative and must be less than the size of the collection \\ r \\ nName parameter . StartIndex" error was. 逐点搜索之后,我发现错误出在requestW类中,其中"The index was off limits. It should not be negative and must be less than the size of the collection \\ r \\ nName parameter . StartIndex"错误是。 ( After checking everything point by point, it seems that this error comes again inside of my class where I'm trying to read an Internet website.) (在逐点检查所有内容之后,似乎该错误再次出现在我尝试阅读Internet网站的班级内部。)

In this class, I have a part who goes from one side of a <tbody to another and after decomposition I see that the StartIndex value is not positive but negative, which throw an exception. 在这个类中,我有一部分从<tbody一侧延伸到另一侧,分解之后,我看到StartIndex值不是正数而是负数,这引发了异常。

So the problem was not in the List<Account> li = (List<Account>)method.Invoke(null, arguments); 所以问题不在List<Account> li = (List<Account>)method.Invoke(null, arguments); itself but in the method class were my requestW is called and gives the Exception. 本身,但在method类中是我的requestW被调用并给出了异常。 Thank you everyone for helping me to solve my problem :) 谢谢大家帮助我解决我的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM