将 Page.Master 转换为 MasterPage 类型来访问/修改控件是否被认为是不好的做法?
将 Page.Master 转换为 MasterPage 类型来访问/修改控件是否被认为是不好的做法? 我一直听说选角很贵。我正在讨论在我继承的这个项目中删除所有转换为母版页类型的实例,并切换到基于事件的模型。 我意识到这有点主观,但我不能是第一个面临这种实现选择的人。我想我正在寻找有经验的意见。
Is casting Page.Master to the MasterPage type to access/modify a control considered bad practice?
I've always heard that casting is expensive. I'm debating gutting all instances of casting to a master page type in this project I've inherited and switching to an event based model.
I realize this is a bit subjective, but I can't be the first to have faced this implementation choice. I'm looking for experienced opinions I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
as
关键字而不是直接转换被认为是更好且“更安全”的做法,除此之外,这样做没有任何问题。如果您经常这样做,只需考虑将其设为带有 getter 的属性,它看起来会更好,因为强制转换似乎与代码的“外观”不太相符。它还可以帮助您避免拼写错误。Using the
as
keyword instead of directly casting is considered a better and a 'safer' practice, other than that, there's no problem doing it. If you do it a lot, just consider making it a property with a getter, it will just look much more nicer as casting doesn't seem to play well with the 'looks' of the code. It may also save you a typo.