Wywoływanie zdarzeń C # spoza klasy właściciela?
Czy jest możliwe w dowolnym zestawie okoliczności, aby to osiągnąć?
Moje obecne okoliczności są następujące:
public class CustomForm : Form
{
public class CustomGUIElement
{
...
public event MouseEventHandler Click;
// etc, and so forth.
...
}
private List<CustomGUIElement> _elements;
...
public void CustomForm_Click(object sender, MouseEventArgs e)
{
// we might want to call one of the _elements[n].Click in here
// but we can't because we aren't in the same class.
}
}
Moją pierwszą myślą było mieć funkcję podobną do:
internal enum GUIElementHandlers { Click, ... }
internal void CustomGUIElement::CallHandler(GUIElementHandler h, object[] args) {
switch (h) {
case Click:
this.Click(this, (EventArgs)args[0]);
break;
... // etc and so forth
}
}
To strasznie brzydkie kludge, ale powinno zadziałać ... Ale musi być bardziej eleganckie rozwiązanie? Biblioteka .NET robi to cały czas, korzystając z funkcji obsługi komunikatów i wywoływania zdarzeń w kontrolkach. Czy ktoś inny ma inne / lepsze pomysły?