在更改以下 VB.net 代码时,我看到了一些奇怪的行为。这是原始源代码:
Private Function ValidateSelectedId(ByVal purposeId As String) As Boolean
Dim possiblePurposes As New InfoCollector.Purpose
Dim isPurposeValid As Boolean = False
'Any of the following purposes (but only these)
'should be considered valid
Select Case UCase(purposeId)
Case UCase(possiblePurposes.FirstPurpose), _
UCase(possiblePurposes.SecondPurpose), _
UCase(possiblePurposes.ThirdPurpose), _
UCase(possiblePurposes.FourthPurpose)
isPurposeValid = True
Case Else
isPurposeValid = False
End Select
Return isPurposeValid
End Function
这是新版本。唯一的变化是增加了第五个有效目的:
Private Function ValidateSelectedId(ByVal purposeId As String) As Boolean
Dim possiblePurposes As New InfoCollector.Purpose
Dim isPurposeValid As Boolean = False
Select Case UCase(purposeId)
Case UCase(possiblePurposes.FirstPurpose), _
UCase(possiblePurposes.SecondPurpose), _
UCase(possiblePurposes.ThirdPurpose), _
UCase(possiblePurposes.FourthPurpose), _
UCase(possiblePurposes.FifthPurpose)
isPurposeValid = True
Case Else
isPurposeValid = False
End Select
Return isPurposeValid
End Function
我在本地 PC 上编译了这个新版本并测试了功能,它运行良好。在将它检查到我们的中央代码存储库并在服务器上构建它之后,使用失败(它似乎忽略了新目的)。在试图弄清楚缺少什么时,我反编译了在服务器上找到的 .DLL,这就是它给我的(注意:我已经更改了变量名并稍微重新格式化):
Private Function ValidateSelectedId(ByVal purposeId As String) As Boolean
Dim ValidateSelectedId As Boolean
Dim possiblePurposes As Purpose = New Purpose()
Dim isPurposeValid As Boolean = False
Dim str As String = Strings.UCase(purposeId)
If (Operators.CompareString(str, Strings.UCase(possiblePurposes.FirstPurpose), False) <> 0) Then
If (Operators.CompareString(str, Strings.UCase(possiblePurposes.SecondPurpose), False) <> 0
AndAlso (Operators.CompareString(str, Strings.UCase(possiblePurposes.ThirdPurpose), False) = 0
OrElse Operators.CompareString(str, Strings.UCase(possiblePurposes.FourthPurpose), False) <> 0)) Then
If (Operators.CompareString(str, Strings.UCase(possiblePurposes.FifthPurpose), False) = 0) Then
Return True
End If
isPurposeValid = False
End If
End If
Return isPurposeValid
End Function
我还尝试将其反编译成 C#,这对我们中的某些人来说可能更容易阅读:
private bool ValidateSelectedId(string purposeId)
{
bool ValidateSelectedId;
Purpose possiblePurposes = new Purpose();
bool isPurposeValid = false;
string str = Strings.UCase(purposeId);
if (Operators.CompareString(str, Strings.UCase(possiblePurposes.FirstPurpose), false) != 0)
{
if (Operators.CompareString(str, Strings.UCase(possiblePurposes.SecondPurpose), false) != 0
&& (Operators.CompareString(str, Strings.UCase(possiblePurposes.ThirdPurpose), false) == 0
|| Operators.CompareString(str, Strings.UCase(possiblePurposes.FourthPurpose), false) != 0))
{
if (Operators.CompareString(str, Strings.UCase(possiblePurposes.FifthPurpose), false) == 0)
{
return true;
}
isPurposeValid = false;
}
}
return isPurposeValid;
}
然而,这似乎与我想要的相反,而不是原始源代码所说的!
我看不出原始源代码是如何编译成这个的。我的反编译器是否有问题(我使用的是 Telerik 的 Just Decompile)?如果是这样,该逻辑如何在我的本地 PC 上运行,而不是在服务器上运行?
编译过程中是否真的存在某种错误?还是我是一个完整的 Id**t,只是误解了反编译代码中的逻辑?
任何解释这一点的相关理论将不胜感激。