MethodInfo.Invoke Throw TargetInvocationException c#

823 views Asked by At

So here is my problem with a TargetInvocationException.

This error occurs on a line with a 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. 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 :

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 :

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

but I still don't understand what it mean... Is the problem with List<Account> ?

Thank you for your help.

1

There are 1 answers

0
Anynahel On BEST ANSWER

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. ( 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.)

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.

So the problem was not in the List<Account> li = (List<Account>)method.Invoke(null, arguments); itself but in the method class were my requestW is called and gives the Exception. Thank you everyone for helping me to solve my problem :)