Similar to how lambda expressions work with free variables, I would like to implement my own closure class, which captures some method parameter.
public class Closure<TObject, TVariable>
{
public TVariable Variable { get; set; }
public Func<TObject, bool> Predicate { get; set; }
}
Then I have a class that works with instances DateTime. One method that this closure class should use is as follows:
public IEnumerable<Item> GetDayData(DateTime day)
{
this.items.Where(i => i.IsValidForDay(day));
}
I would like to convert it to a class Closure. The problem is that I would like to reuse (for performance reasons) my class instance Closure:
private Closure<Item, DateTime> closure = null;
public IEnumerable<Item> GetDayData(DateTime day)
{
if (closure == null)
{
this.closure = new Closure<Item, DateTime>() {
Variable = reference of "day" param, <=== HOW ????????
Predicate = i => i.IsValidForDay(this.closure.Variable)
}
}
this.items.Where(this.closure.Predicate);
}
Closure, ( Variable), . , , Variable, , .
?
, , lambda . ( Reflector), , , , ...