【问题标题】:No "using" reference in asp.net generated code在 asp.net 生成的代码中没有“使用”参考
【发布时间】:2011-11-13 02:20:00
【问题描述】:

当我查看生成的 aspx 页面代码时,有一件事让我印象深刻。每次都会打印每个类引用,而不是在代码文件顶部应用“使用”方法。
这样做有什么理由吗?
如果这两种方法没有区别,那么为什么不使用“使用”来简单呢?

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

对比

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

【问题讨论】:

  • +1,我自己也想过这个问题。

标签: asp.net code-generation


【解决方案1】:

可能有助于避免类型歧义。

更新:Heinzi 描述得更优雅。

【讨论】:

    【解决方案2】:

    没有理由像这样完全限定类型名称,除非您为了清楚起见遵循某种编码标准。

    【讨论】:

      【解决方案3】:

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

      编写的简单性是一个问题,但是:如果类型总是用它们的完全限定名称来指定,那么名称冲突的可能性就会降低。假设您有两个提供 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();
      

      【讨论】:

      • +1 但我想我会说阅读的简单性不是优先事项而不是问题。有时您想阅读生成的代码,如果它稍微干净一点将不胜感激,但这并不是什么大不了的事,因此所需的时间对于微小的好处来说是不值得的。
      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2014-09-02
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多