-1

这是将字符串简单地编码为正则表达式中可接受的文字的编码器。与. Option Infer On_ Option Infer Off_ MissingMemberException: No default member found for type VB$AnonymousDelegate_1(Of Char, String)

LINQ 表达式是否可以重写以使用Option Infer Off? 我想保持关闭,因为我认为Option Infer On大型遗留项目/库存在风险。(或者我错了吗?)

这是该方法的代码:

Shared Function EscapeLiteralForRegexPattern(inputString As String) As String

  '*** Based on Escape(). Keep them in sync.
  Const dontEscapeFrom = &H20
  Const dontEscapeTo = &H7E
  If String.IsNullOrEmpty(inputString) Then Return inputString

  Const escaper As String = "\"
  Const escapePattern As String = escaper & escaper
  Const tabPattern As String = escaper & "t"
  Const crPattern As String = escaper & "r"
  Const lfPattern As String = escaper & "n"
  Const unicodePattern As String = escaper & "u{0:X4}"
  Const asciiPattern As String = escaper & "x{0:X2}"

  Dim ConvertChar = Function(c As Char) As String
             If c = escaper Then Return escapePattern
             Dim ac As Integer = AscW(c)
             If ac >= dontEscapeFrom AndAlso ac <= dontEscapeTo Then
               Return Regex.Escape(c)
             End If
             Dim result As String = String.Format(If(ac < &H80, asciiPattern, unicodePattern), ac)
             If dontEscapeFrom = 0 Then Return result
             Select Case c
               Case vbTab
                 Return tabPattern
               Case vbCr
                 Return crPattern
               Case vbLf
                 Return lfPattern
               Case Else
                 Return result
             End Select
           End Function

  Return String.Join(String.Empty, inputString.Select(Function(c) ConvertChar(c)).ToArray())

End Function

这是我尝试过的 LINQ 更改(但问题仍然存在):

  Return String.Join(String.Empty, inputString.Select(Function(c As Char) As String
                              Return ConvertChar(c)
                            End Function).ToArray())
4

1 回答 1

1

归功于汉斯·帕桑特- 见他的评论。

我将代码添加到需要有的遗留庞大库中Option Explicit OffOption Strict Off直到重写。

我忘记了它们已关闭,因此在编码过程中错过了一些明确的类型声明。在所有要求的更正之后Option Explicit OnOption Strict On整个问题MissingMemberException都消失了。

于 2014-11-15T23:38:10.290 回答