I have a class like so.
public class Step<T>
{
public delegate void Act<T>();
Act<T> _act;
public Step(Act<T> action)
{
Contract.Requires(action != null);
_act = action;
}
public void PerformStep()
{
_act.Invoke();
}
}
My problem is as follows. I want the constructor of this class to be able to accept a delegate with the definition you see in the class. What I do not want is for that delegate to be a public member of my class. The alternative of this is to have a public delegate outside of the class but no other class but this one will need it. Is there a way I can make the delegate private?
If that solution is not possible is there an alternative? This step class is just a wrapper for delegates that are to be run sequentially. There is no need for a signature if I can have a more generic solution.
EDIT: More info for clarification.
The aim of this class it to be able to allow the user to do something like the following.
If I have a Class Car. This car will have methods like Drive, Stop, Turn etc. Using this Step Class the user of the Class should be able to create an instance of their class then do the following.
Car c = new Car();
Step<Car> step1 = new Step<Car>(c.TurnLeft);
Step<Car> step2 = new Step<Car>(c.Drive);
Step<Car> step3 = new Step<Car>(c.TurnRight);
//add steps to a collection
c.AddNewRoutine(collection of steps);
c.RunRoutine(identify which routine);
The steps will be added to a data structure that will allow them to be executed in order. The car class will contain a data structure that can hold these groups of steps. This then allows any class to be created and have the user set up steps for that class to take, without the class or main program having to hardcode the behaviour.
To finish. I need the step class to be as it is. I just do not want to allow public access to that delegate in any form as the only class who needs it is the Step class.
Remove the delegate alltogether and use an Action:
Edit:
you can also have a simple
System.Actionwith no type parametersHowever, keep in mind that when doing this:
your’re tying the step1 to the actual instance
c, you will not be able to reuse that step for another instance: