这是字符串的简单编码器,在正则表达式中是可接受的。适用于Option Infer On
,但在Option Infer Off
生成的朗姆酒中崩溃
MissingMemberException: No default member found for type VB$AnonymousDelegate_1(Of Char, String)
。
即使使用Option Infer Off
,LINQ表达式是否可以重写?我想保留它,因为我认为在大型遗留项目/库中存在转换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())
答案 0 :(得分:1)
信用转到 - 请参阅他的评论。
我将代码添加到遗留的庞大库中,需要Option Explicit Off
,Option Strict Off
直到重写。
我忘记了它们已关闭,因此在编码期间错过了一些显式类型声明。在Option Explicit On
和Option Strict On
以及所有请求的更正后,MissingMemberException
的整个问题都消失了。