这是将字符串简单地编码为正则表达式中可接受的文字的编码器。与. 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())