【问题标题】:Extending Object does require an import扩展对象确实需要导入
【发布时间】:2013-08-01 14:32:48
【问题描述】:

我找到了很多关于使用System.Runtime.CompilerServices 扩展类的信息。这很好用,但现在我已将此代码添加到我的类库中,从现在开始我需要一个 import 才能工作。

一个简单的例子:

Public Module StringHelpers
    <Extension()> _
    Public Function customLengthPlus1(ByVal dt As String) As Integer
        Return dt.Length + 1
    End Function
End Module

是否可以在 web.config 或 IIS 中添加一行,这样我就不需要在所有文件和项目中使用 import

我已经在 web.config 中的 pages -&gt; controls 下导入了我的类库。

【问题讨论】:

    标签: asp.net .net vb.net web-config


    【解决方案1】:

    您无法在代码隐藏类(.cs 文件)中避免这种情况,但 aspx 页面(或 cshtml,如果使用 MVC)除外,在这种情况下,您只需在 web.config 上添加命名空间

    <configuration>
     <system.web>
        <pages>
          <namespaces>
            <add namespace="Your.Namespace" />
            ...
          </namespaces>
        </pages>  
      </system.web>
    </configuration>
    

    【讨论】:

    • @KarlAnderson 对不起,伙计。我在 asp.net 中追求我的金徽章;)呵呵
    • 别担心,一切都是为了帮助人们。
    【解决方案2】:

    对于.aspx 页面,您可以将项目添加到 web.config,如下所示:

    <configuration>
        <system.web>
            <pages>
                <namespaces>
                    <add namespace="System.Data" />
                    <add namespace="System.Text" />
                    <add namespace="System.Runtime.CompilerServices" />
                </namespaces>
            </pages>  
        </system.web>
    </configuration>
    

    但是对于代码隐藏,没有办法绕过这个 AFAIK。

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 2012-11-29
      • 2019-02-17
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      相关资源
      最近更新 更多