.NET 4.5包含一个名为as的新验证属性CreditCardAttribute,该属性指定数据字段值是信用卡号.当我反编译包含此类的程序集时,我可以看到以下代码用于信用卡号验证:
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string text = value as string;
if (text == null)
{
return false;
}
text = text.Replace("-", "");
text = text.Replace(" ", "");
int num = 0;
bool flag = false;
foreach (char current in text.Reverse<char>())
{
if (current < '0' || current > '9')
{
return false;
}
int i = (int)((current - '0') * (flag ? '\u0002' : '\u0001'));
flag …Run Code Online (Sandbox Code Playgroud)