这很好用:
protected void txtTest_Load(object sender, EventArgs e)
{
if (sender is TextBox) {...}
}
有没有一种方法来检查发件人是否不是TextBox,某种等同于“ is”的!=?
请不要建议将逻辑移至ELSE {} :)
Answers:
这是一种方法:
if (!(sender is TextBox)) {...}
if (false == sender is TextBox)
false
比否定起来更清晰。单词的使用is
可能会引起足够的视觉干扰,以至于只看一眼代码就很容易错过!
您是否也可以在is
关键字之前做更冗长的“旧”方式:
if (sender.GetType() != typeof(TextBox)) { // ... }
尝试这个。
var cont= textboxobject as Control;
if(cont.GetType().Name=="TextBox")
{
MessageBox.show("textboxobject is a textbox");
}
as Control
行的行为与GetType()
上的行为不同object
?除此之外,类似这样的字符串比较不是重构友好代码。