Andrey
Andrey

Reputation: 347

How can I create a class whose object takes a function in the constructor and can execute it?

I want to create a class whose object takes some function in the constructor and can then produce it. What can I use for this?

This is for a class that will have the name of the command and the function it will perform. I want to create an object of this class to transfer to it a function that it will perform.

class Function
 {
    public Function() // here I want to pass the method that I want to execute
    {

    }
    private void Execute()
    {
        //SomeMethod();
    }
  }

class Command
 {
    private string _name;
    private Function SomeMethod;

    public Command(string name)
    {
        _name = name;
    }

 }

Upvotes: 1

Views: 76

Answers (1)

Denis Schaf
Denis Schaf

Reputation: 2776

something like this will do the trick for you. This shows the syntax to create a function that takes a function that takes a string and integer.

public void CallMeIWillCallYourFunction(Action<string,int> yourFunction)
{
    yourFunction("HelloWorld", 123);
}

Upvotes: 4

Related Questions