0

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.

Anynahel
  • 41
  • 6
  • Since it seems you're debugging code compiled on-the-fly, I'm going to assume you have no debugging information. You'll have to manually comb through the dynamically compiled code to see where the `IndexOutOfRangeException` can possibly happen, and use old-school debugging techniques to narrow it down (e.g. `Debug.WriteLine` at useful places). – Luaan Feb 15 '16 at 09:39
  • what do you get at `method`? – Amit Kumar Ghosh Feb 15 '16 at 09:41
  • `CodeCompiler` is an abstract class. How come you create an instance of it? Are you not targeting `System.CodeDom.Compiler` namespace? – Amit Kumar Ghosh Feb 15 '16 at 09:50
  • Luaan : I will try with Debug.WriteLine to see where it comes exactly. For the moment I m trying to see if the problem do not comes from the `method`. Amit Kumar Ghosh : At `method` I get my dynamic compilation ( which works totally ) from my `public MethodInfo CompileCode(string code, string namespacename, string classname,string functionname, bool isstatic, params object[] args)` class. – Anynahel Feb 15 '16 at 09:57
  • Ivan : all the arguments are good, I checked it, with the debug mode. My `method` needs `(string code, string namespacename, string classname, string functionname, bool isstatic, params object[] args)` and all of thoses arguments are not null and specified. Amit Kumar Ghosh : I have made a `public MethodInfo CompileCode ` who needs the `System.CodeDom.Compiler;` assembly. I'm not targeting System.CodeDom.Compiler namespace but mine who is `test`. I am going to see if the problem is not in in `List` or in the page I am trying to dynamically compilating who is `requestW`. – Anynahel Feb 15 '16 at 10:03

1 Answers1

1

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 :)

Anynahel
  • 41
  • 6