Chcę aby na początku danego kodu dodawała się do pewnego eventu funkcja, a na koniec zostawała usuwana, także w przypadku wyjątków. Najbezpieczniejszym sposobem jest oczywiście użycie using, więc napisałem sobie następującą, klasę:
public class SafeEvent<T1> : IDisposable
{
Action<T1> what;
Action<T1> where;
public SafeEvent(ref Action<T1> where, Action<T1> what)
{
this.what = what;
this.where = where;
this.where += this.what;
}
public void Dispose()
{
this.where -= this.what;
}
}
Jednak problem jest taki że w praktyce where z jakiegoś powodu nie jest przekazywane przez referencję :\
Dlaczego tak się dzieje i jak to poprawić?