我想创建一个返回新对象并将委托作为参数的方法。委托应使用该对象进行操作。我不想将该对象作为参数并使用返回我的函数的对象。是否可以使此代码运行?
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();
}