0

Ok, So I'm felling super lazy and I was wondering if I can call a function dynamically by a function pointer or something else somehow?

Instead of writing a bunch of if else I can put all possible functions in an array and pass which index I wan't to execute.

I was thinking of some sort of linkedlist. eg.

    //mainClass
    private void initFunctionLL()
    {
        currNode.functionRef = this.funct1;
        ...
        nextNode.functionRef = this.funct2;
    }
    private void callNext(){
        currNode = currNode.Next();
        currNode.execute();
    }
    //
    //nodeClass
    public void execute()
    {
        call myFunctionRef();
    }
Hasnain Bukhari
  • 458
  • 2
  • 6
  • 23
user3532232
  • 257
  • 8
  • 19
  • 3
    **[Delegates](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/)** – SᴇM May 24 '18 at 10:07
  • **[Reflection](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection)** – SᴇM May 24 '18 at 10:10

2 Answers2

2

If your functions has the same return type and the same parameters list then you can use Func<T> or Action to make a set of delegates to this functions and call it. Example for functions without parameters which don't return a value:

private void ExecuteManyFunctions()
{
    List<Action> actions = new List<Action>();
    actions.Add(Foo);
    actions.Add(Bar);
    foreach(var func in actions)
        func();
}

private void Foo() => { // some logic here }
private void Bar() => { // some logic here }

Another example for functions with integer parameter returns string:

private void ExecuteManyFunctions()
{
    List<Func<string, int>> actions = new List<Func<string, int>>();
    actions.Add(Foo);
    actions.Add(Bar);

    var results = new List<string>();
    foreach(var func in actions)
        results.Add(func(1));
}

private string Foo(int x) => { return x.ToString(); }
private string Bar(int y) => { return "staticResult"; }
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
0

You can add a bunch of Func or Action delegates to a list and then call each one. If your method takes no arguments and returns nothing, then use Action, if it takes one argument and returns nothing then use Action<T> wherein T specifies the type of argument. If it returns something then use Func<T>, wherein T specifies the return type. In Func<T> the last item specifies the return type and the ones before specify the argument types. Please see links at then end of my answer for more details. There are many Action and Func delegates with variable parameters. If none of them are satisfy your needs, then look into Delegate.

For example, in example below I am declaring a list which will hold a bunch of funcs which take one int and return one int. Then I am looping through and calling each one. First one multiplies the number passed to it and return the product, while the 2nd one adds the number to itself.

var funcs = new List<Func<int, int>>();
funcs.Add(x => x * x);
funcs.Add(x => x + x);
funcs.Add(x => Square(x)); // Or like this

foreach (var thisFunc in funcs)
{
    thisFunc(5);
}

private static int Square(int number)
{
    return number * number;
}

Please see Func and Action.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64