0

I have a Dictionary<string, Delegate> commands with my delegate taking two strings and a string[] as params.

I'm entering a command in a Console Application:

:adduser username [email protected] Firstname Middlename Lastname

where everything is converted to an array of strings (size 6). I then do

list[3] = string.Join(" ", input, 3, 3);
list.RemoveRange(4, list.Count - 4);

to convert Firstname, Middlename, and Lastname into a single string, so that my array looks like this:

input[0]: :adduser

input[1]: :username

input[2]: :[email protected]

input[3]: :Firstname Middlename Lastname

I then try to call commands[input[0]].Method.Invoke(this, list.Skip(1).ToArray<object>());, but it results in an ArgumentException with the message

Object of type 'System.String' cannot be converted to type 'System.String[]'.

I'm a bit lost, as I don't really know what's wrong. I'd appreaciate any help!

0

3 Answers 3

1

You perhaps did mean

commands[input[0]].Method.Invoke(this, new object[] { list[0], list[1], list.Skip(2).ToArray()});

That should match your delegates signature which expect 3 arguments. Your error message indicates that your list is passed as first argument. The reason is that you must pass the other arguments as an object array where the first item is arg1, the second is arg2, ....

Sign up to request clarification or add additional context in comments.

Comments

1

Method.Invoke takes an array of parameters after the instance. When you have to pass an array as a single parameter, you need to wrap it in an object[]. Otherwise it gets interpreted as a set of parameters to the delegate:

commands[input[0]].Method.Invoke(this, new object[] {list.Skip(1).ToArray()});

Comments

1

Your problem is in list[3] = string.Join(" ", input, 3, 3);. If your signature is in fact (string user, string mail, string[] names) you can not call it with 3 strings.

My proposal:

list[3] = input.Skip(3).Take(3).ToArray();

PS: Regarding design I do not recommend recycling the list object but rather create a dedicated object that carries the arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.