I finally used generics

As the straight left

Generics are. NET2. The new thing that comes out of 0. Shame, 3. The 0 has been out for a long time, but I’m just starting to use it now. Backward backward. It’s a pile of shit.

delegate void SetEnableCallback(System.Windows.Forms.Control objCtrl, bool enable);

 

/ / declare

private void _SetText<TObject>(TObject objCtrl, string text)

where TObject : System.Windows.Forms.Control

{

if (objCtrl.InvokeRequired)

   {

       SetTextCallback d = new SetTextCallback(_SetText);

       this.Invoke(d, new object[] { objCtrl, text });

   }

   else

   {

       objCtrl.Text = text;

   }

}

 

/ / call

public int TotalNum

{

set { _SetText<TextBox>(this.txt_Total, value.ToString()); }

}

 

In the declaration, the reason for limiting TObject to a form control is that it is a form control

where TObject : System.Windows.Forms.Control

Otherwise, statements such as objCtrl.InvokeRequired that are only used by forms controls cannot be used.

TextBox; Button; TextBox; Button;

private void _SetText<TObject>(TObject objCtrl, string text)

where TObject : TextBox,Button

Class type can only be ranked in front, TextBox is ranked in front, say Button wrong; TextBox = Button; TextBox = Button; I don’t know why there is such a rule. I ended up limiting them to the parent type: Control.

 

The nice thing about generics is that you can write extremely adaptable code. It is said that the efficiency is very high, no type conversion, boxing and unboxing performance loss.

The big river goes east, the waves are gone,

Heroes of the past.

To the west, humanity is,

Three Kingdoms Zhou Lang chibi.

The rocks are broken, the waves are breaking,

Roll up a thousand pieces of shit.