I am looking for a way to unfold a params object array casting to the correct argument type based on a given function signature as demonstrated in the following example:
public class Class {
public enum State { A, B, /* ...*/}
public void GenericFunction(State state, params object[] args) {
switch(state) {
case State.A: Apply(CaseA,args); break;
case State.B: Apply(CaseB,args); break;
/* ... */
}
}
public void CaseA(int i, string s) { /* ... */ }
public void CaseB(double[] ds) { /* ... */ }
public void ExampleInvocation() {
GenericFunction(State.A,10,"abc"); // supposed to call CaseA with arguments 10 and "abc"
GenericFunction(State.B,new double[] { 1.2, 3.5, 7.2}); // supposed to call CaseB
GenericFunction(State.A,6.66); // supposed to throw an exception
}
}
Is there a library or some feature in c# providing something like the method Apply
?