这是我的代码:
public class MyButton
{
Object button;
public MyButton(System.Windows.Forms.ToolStripButton button)
{
this.button = button;
}
public MyButton(System.Windows.Forms.ToolStripSplitButton button)
{
this.button = button;
}
public void EnableButton(bool enable)
{
if (button is System.Windows.Forms.ToolStripButton)
((System.Windows.Forms.ToolStripButton)button).Enabled = enable;
else if (button is System.Windows.Forms.ToolStripSplitButton)
((System.Windows.Forms.ToolStripSplitButton)button).Enabled = enable;
}
//...
}
我想知道我可以让这段代码更短吗?我可以以某种方式按其类型投射它吗?像这样的东西:
public void EnableButton(bool enable)
{
((FakeFuctionToGetCastType(button))button).Enabled = enable;
}
当然这是我的假功能......那么有没有办法做到这一点?