Dan*_*ner 27
因为这会引起很多问题......
public void DoDarkMagic(ref Control control)
{
control = new TextBox();
}
public void Main()
{
Button button = new Button();
DoDarkMagic(ref button);
// Now your button magically became a text box ...
}
Run Code Online (Sandbox Code Playgroud)
你可以用泛型来解决一些打字限制.
void Test<T>(ref T control)
where T: Control
{
}
Run Code Online (Sandbox Code Playgroud)
现在你可以打电话:
Button b = new Button()
Test(b);
Run Code Online (Sandbox Code Playgroud)
您可以将任何类型的引用传递给它,该引用来自控件.
现实生活场景:
protected static void BindCollection<T>(
T list
, ref T localVar
, ref ListChangedEventHandler eh // the event handler
, ListChangedEventHandler d) //the method to bind the event handler if null
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
{
//woot! a one line complex setter
BindCollection(value, ref this._Children, ref this.ehchildrenChanged, this.childrenChanged);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5020 次 |
| 最近记录: |