C # Design and Application of generics and reflection

Source: Internet
Author: User

From magician s blog
There will always be a fixed set of buttons and operation conventions in the design of the device Operation Panel, but it is very inefficient to write the same set of code for every project.

Most device operation buttons include start, pause, and stop buttons. These three buttons are also required on the software interface.

To disable or enable different button combinations. For example, when the software is started, the start button is enabled, the pause and stop buttons are disabled, and so on.

In general design, a Button is used, and some design uses ToolStripButton. to make it compatible with more controls, a generic type is used. The Code is as follows:


Public enum OperateState {Run, Pause, Stop };

Public class OperateStateRule <T> where T: ToolStripItem
{
Private T start;
Private T pause;
Private T stop;

Private OperateState state = OperateState. Stop;

Private OperateState State
{
Get {return state ;}
// Set {state = value ;}
}


Public OperateStateRule (T _ start, T _ pause, T _ stop)
{
Start = _ start;
Pause = _ pause;
Stop = _ stop;

Update (OperateState. Stop );
}

Public void Update (OperateState _ state)
{
State = _ state;
Try
{
Switch (state)
{
Case OperateState. Stop:
Start. Enabled = true;
Pause. Enabled = false;
Stop. Enabled = false;
Break;
Case OperateState. Run:
Start. Enabled = false;
Pause. Enabled = true;
Stop. Enabled = true;
Break;
Case OperateState. Pause:
Start. Enabled = true;
Pause. Enabled = false;
Stop. Enabled = true );
Break;
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
}

Call example:

ToolStripButton btnStart;
ToolStripButton btnPause;
ToolStripButton btnStop;

// Bind the corresponding button when generating the object
OperateStateRule <ToolStripButton> rule = new OperateStateRule <ToolStripButton> (btnStart, btnPause, btnStop );
// Update the status in the Click Event
Private void btnStart_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Run );
}

Private void btnPause_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Pause );
}

Private void btnStop_Click (object sender, EventArgs e)
{
Rule. Update (OperateState. Stop );
}

There is a where T: ToolStripItem statement in the class declaration. This is a c # type constraint. If this constraint is not added, the compiler reports an error and cannot find the Enabled attribute,

However, there is a problem that the buttons and ToolStripButton classes are not derived from the same base class, so they cannot be compatible with the two classes, because this constraint must be imposed to make c ++

Some generic practices are restricted.

There is still no better way to achieve the desired effect. The answer is yes, that is, reflection. See the following improved code example:

Public enum OperateState {Run, Pause, Stop };

Public class OperateStateRule <T>
{
Private T start;
Private T pause;
Private T stop;

Private OperateState state = OperateState. Stop;

Private OperateState State
{
Get {return state ;}
// Set {

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.