You can get around some input restrictions with generics.
void Test<T>(ref T control)
where T: Control
{
}
Now you can call:
Button b = new Button()
Test(b);
You can pass a link of any type to it, which is inferred from the control.
Real life scenario:
protected static void BindCollection<T>(
T list
, ref T localVar
, ref ListChangedEventHandler eh
, ListChangedEventHandler d)
where T : class, IBindingList
{
if (eh == null)
eh = new ListChangedEventHandler(d);
if (list != null && list != localVar)
{
if (localVar != null)
localVar.ListChanged -= eh;
localVar = list;
list.ListChanged += eh;
}
else if (localVar != null && list == null)
{
localVar.ListChanged -= eh;
localVar = list;
}
}
public override BindingList<ofWhatever> Children
{
get{
set
{
BindCollection(value, ref this._Children, ref this.ehchildrenChanged, this.childrenChanged);
}
}
source
share