VB.Net扩展功能不是字符串的成员

时间:2019-02-04 23:15:58

标签: asp.net vb.net extension-methods

我试图将整个asp.net应用程序中的某些常用功能折叠到某些字符串扩展方法中,但是却遇到了一个问题,即我们的网站正在踢出方法不是String成员的编译器错误。

这是我创建的实用程序名称空间中的模块定义:

Imports System
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
Imports System.Data
Imports System.Collections.Generic
Imports System.Data.SqlClient

Namespace Utilities    
    'This extension extends the String class with new functions to check string validity.  Add any such new functions here
    <Extension()> _
    Public Module StringExtensions
        <Extension()> _
        Public Function IsAlphaNumeric(targ As String) As Boolean
            Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[a-zA-Z0-9]+$")
        End Function

        Public Function IsNumeric(targ As String) As Boolean
            Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[0-9]+$")
        End Function

        Public Function RegexRemove(targ As String, rpl As String) As String
            Return Regex.Replace(targ, rpl, "")
        End Function
    End Module
End Namespace

这是我尝试从中调用扩展方法的位置以及对其中一个方法的调用示例:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Collections.Generic
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI
Imports System.IO
Imports System.Data.SqlClient
Imports System.Runtime.CompilerServices
Imports Utilities
Imports Utilities.StringExtensions
Imports Utilities.PageOps
Imports Utilities.DBChecks

Public Class UserDashboard
    Inherits System.Web.UI.Page

    '...

    Private Function GetRoleLst(rMap As Collections.Generic.Dictionary(Of String, String)) As List(Of String)
        Dim rLst As New List(Of String)()

        For Each kvp As KeyValuePair(Of String, String) In rMap
            For Each con As Control In ThRolePanel.Controls
                If TypeOf con Is CheckBox Then
                    Dim conID As String = con.ID
                    If DirectCast(con, CheckBox).Checked And conID.RegexRemove("\s") = kvp.Key Then
                        rLst.Add(kvp.Value)
                    End If
                End If
            Next
        Next
    End Function
End Class

我们的Web.config文件指向.net 4.0,并且System.Core已添加到程序集列表中。这是编译选项卡:

<compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
      <assemblies>
         <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="Telerik.ReportViewer.WebForms, Version=8.2.14.1204, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" />
        <add assembly="Telerik.Reporting.Service, Version=8.2.14.1204, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" />
        <add assembly="Telerik.Reporting, Version=8.2.14.1204, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" />
      </assemblies>

      <buildProviders>
        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
      </buildProviders>
    </compilation>

已经浏览了许多SO问题,有关VB扩展功能的指南以及.net文档,但到目前为止,这些都没有帮助我弄清楚这一点。


更新

因此,我直觉地尝试通过这样的命名空间来访问扩展方法:

  

Utilities.RegexReplace(con.ID,“ \ s”)

可以,但是如果我尝试以String的扩展名访问它,我仍然遇到上一个问题,即它报告“ RegexReplace”不是字符串错误的成员。似乎出于某种原因,扩展方法实际上并未扩展String。

1 个答案:

答案 0 :(得分:1)

您已将Extension属性添加到IsAlphaNumeric,但未添加到IsNumericRegexRemove。这个:

<Extension()> _
Public Function IsAlphaNumeric(targ As String) As Boolean
    Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[a-zA-Z0-9]+$")
End Function

Public Function IsNumeric(targ As String) As Boolean
    Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[0-9]+$")
End Function

Public Function RegexRemove(targ As String, rpl As String) As String
    Return Regex.Replace(targ, rpl, "")
End Function

应该是这样:

<Extension()> _
Public Function IsAlphaNumeric(targ As String) As Boolean
    Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[a-zA-Z0-9]+$")
End Function

<Extension()> _
Public Function IsNumeric(targ As String) As Boolean
    Return System.Text.RegularExpressions.Regex.IsMatch(targ, "^[0-9]+$")
End Function

<Extension()> _
Public Function RegexRemove(targ As String, rpl As String) As String
    Return Regex.Replace(targ, rpl, "")
End Function