加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在asp.net生成的代码中没有“使用”引用

发布时间:2020-12-16 09:23:03 所属栏目:asp.Net 来源:网络整理
导读:当我查看生成的aspx页面代码时,有一件事让我感到震惊.每次打印每个类引用,而不是在代码文件的顶部应用“using”方法. 这样做是否有理由? 如果两种方法没有区别那么为什么不使用“使用”来简化? System.Data.DataSet theSet = new DataSet(); VS using Syst
当我查看生成的aspx页面代码时,有一件事让我感到震惊.每次打印每个类引用,而不是在代码文件的顶部应用“using”方法.
这样做是否有理由?
如果两种方法没有区别那么为什么不使用“使用”来简化?

System.Data.DataSet theSet = new DataSet();

VS

using System.Data;  
DataSet theSet = new DataSet();

解决方法

因为对于生成的代码,读取的简单性不是优先考虑的.

然而,写作的简单性是一个问题:如果始终使用完全限定名称指定类型,则名称冲突的可能性会降低.想象一下,您有两个提供TextBox控件的库,并将它们添加到Web表单中.

// no problem
System.Web.UI.WebControls.TextBox myDefaultTextBox = new System.Web.UI.WebControls.TextBox();
CustomLibrary.TextBox theOtherTextBox = new CustomLibrary.TextBox();

相比于

using System.Web.UI.WebControls;  
using CustomLibrary;  

// won't compile,would need special treatment by the code generator
TextBox myDefaultTextBox = new TextBox();
TextBox theOtherTextBox = new TextBox();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读