I want to create a method which returns new object and takes delegate as parameter. Delegate should operate with that object. I would like to not put that object as parameter and use object that returns my function. Is it possible to make this code running?
public class ActionContext
{
public Action Action;
public int Variable = 0;
}
public ActionContext Create(Action action)
{
return new ActionContext(){ Action = action };
}
public void Test()
{
// I don't want provide ActionContext through delegate(ActionContext)
ActionContext context = Create(delegate
{
//ERROR: Use of unassigned local variable 'context'
context.Variable = 10;
});
context.Action.Invoke();
}