2

There is a set of methods like:

  Foo(int, float, params objects[])
  Goo(int, params objects[])
  Too()

each taking different number of & type of parameters (so are return values).

I read an integer (an index) from a database. The integer corresponds to one of the above method (1 for Foo, 2 for Goo and 3 for Too).

How do I store above methods (as delegates) in a collection so that I can call appropriate method by indexing into the collection using the integer read from db as index.

4 Answers 4

8

You could just use a Delegate[] - when you read the integer, you'll then need to cast the relevant array value to an Action<int, float, object[]>, an Action<int, object[]> or an Action before you call it though.

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

2 Comments

You do not need to cast the delegate before invoking it - just use DynamicInvoke
@Dror Helper: You could use dynamic invocation, but personally I'd rather find out the type is wrong immediately via a cast.
6

Store the methods as Delegate inside a List or Array and use DynamicInvoke to call them - note that you will need to pass the correct number (and type) of variables.

var delegates = new List<Delegate>();
delegates.Add((Action<int, float, object[]>)Foo);
//...
delegates[0].DynamicInvoke(new[] {0, 1.2, new object()});

This solution is best suited when you want to pass default (read: zero/null) to the methods - then you can use reflection to create the default values.

Another useful case is if you want to call a specific method signature (i.e. a method that receives a single int) then you can go to the desired index and check (using reflection) if the delegate can be called with that argument.

Otherwise you need to have some knowledge of the method you're calling because each method has different argument type and number.

2 Comments

Very nice solution, but still the ifs are inevitable as you need to know the number of parameters.
True - but we can write code using reflection that would not be dependent on the method name. It depends on what you're trying to accomplish here.
4

As the three methods have different number of arguments and types in order to call them you will need to know in which case you are, so the ifs are inevitable. IMHO there will be no benefit to hold an array of delegates.

switch (index) {
    case 1: // call method1
        break;
    case 2: // call method2
        break;
    case 3: // call method3
        break;
}

1 Comment

How this answers the question above?
0

I would suggest using a dictionary where the key is the index and the value is the delegate. However the methods have different signatures, so you will not be able to directly use them interchangeably. By the way where would you take the parameters for the methods when calling them?

1 Comment

I currently have switch case. But that does not look good. I will try delegate array as suggested in 2 posts earlier. by the way, I read the parameters from a database. The database contains the methods IDs & their parameters. Ex: InputCmd: Show News Method ID: 1 EventIdValid: EventId: StateIdValid: StateId: NumVarArgs: VarArgs1: VarArgs2: . . . VarArgs10: Depending on the input command, different methods with associated params will be called.

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.