我刚刚在 .NET 基础库的反射器中找到这段代码...
if (this._PasswordStrengthRegularExpression != null)
{
this._PasswordStrengthRegularExpression = this._PasswordStrengthRegularExpression.Trim();
if (this._PasswordStrengthRegularExpression.Length == 0)
{
goto Label_016C;
}
try
{
new Regex(this._PasswordStrengthRegularExpression);
goto Label_016C;
}
catch (ArgumentException exception)
{
throw new ProviderException(exception.Message, exception);
}
}
this._PasswordStrengthRegularExpression = string.Empty;
Label_016C:
... //Other stuff
我听过所有关于“你不能因为害怕永远流放到 hell 而使用 goto”的说法。我一直非常尊重 MS 编码人员,虽然我可能不同意他们的所有决定,但我始终尊重他们的推理。
那么 - 是否有充分的理由让我缺少这样的代码?这段代码摘录是不是由无能的开发人员拼凑而成的?还是 .NET 反射器返回不准确的代码?
我希望有一个很好的理由,但我只是盲目地错过了它。
感谢大家的参与
最佳答案
反射器并不完美。此方法的实际代码可从 Reference Source 获得。它位于 ndp\fx\src\xsp\system\web\security\admembershipprovider.cs:
if( passwordStrengthRegularExpression != null )
{
passwordStrengthRegularExpression = passwordStrengthRegularExpression.Trim();
if( passwordStrengthRegularExpression.Length != 0 )
{
try
{
Regex regex = new Regex( passwordStrengthRegularExpression );
}
catch( ArgumentException e )
{
throw new ProviderException( e.Message, e );
}
}
}
else
{
passwordStrengthRegularExpression = string.Empty;
}
请注意它是如何未能检测到最后一个 else 子句并使用 goto 对其进行补偿的。它几乎肯定是被 if() 语句中的 try/catch block 绊倒了。
很明显,您会更喜欢实际的源代码而不是反编译的版本。评论本身很有帮助,您可以相信来源是准确的。好吧,大部分是准确的,有一些小的损坏来自一个有缺陷的后处理工具,该工具删除了 Microsoft 程序员的名字。标识符有时会被破折号代替,并且代码会重复两次。可以下载源here .
关于c# - 是否有理由在现代 .NET 代码中使用 goto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2542289/