這是字符串簡單的編碼器,可以在正則表達式中使用。適用於Option Infer On
,但在012umrumtime崩潰,生產 MissingMemberException: No default member found for type VB$AnonymousDelegate_1(Of Char, String)
。可以LINQ重寫以避免MissingMemberException和'Option Infer Off'?
即使在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())
Downvoters:請您留下反饋問題有什麼問題? – miroxlav
你的代碼有一個錯誤,你正在比較蘋果和橙子。通過將Option Strict On放在源代碼文件的頂部來進行細分。使用Option Infer On,編譯器可以將蘋果變成橙色。如果你把它關掉,不,你得到蘋果醬。 –
不是我,但你想複製[RegEx.Escape](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape%28v=vs.110% 29.aspx)? –