我有一个这样的移动课程:
public class Mobile
{
private string region;
private string number;
private bool regionIsValid;
private bool numberContentIsValid;
private string internationalNumber;
public string Region
{
set { region = value.ToUpper(); ConvertNumber(); }
get { return region; }
}
public string Number
{
set { number = value; ConvertNumber(); }
get { return number; }
}
public Interface Reason
{
set { reason = value; }
get { return reason; }
}
public bool NumberContentIsValid
{
set { numberContentIsValid = value; }
get { return numberContentIsValid; }
}
public string InternationalNumber
{
set { internationalNumber = value; }
get { return internationalNumber; }
}
public void ConvertNumber()
{
var sw = Stopwatch.StartNew();
try
{
if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false)
return;
var phoneUtil = PhoneNumberUtil.GetInstance();
var phone = phoneUtil.Parse(Number, Region);
InternationalNumber = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL);
Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds));
return;
}
catch (Exception ex)
{
Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds));
return;
}
}
public bool DoesNumberContentValid()
{
var sw = Stopwatch.StartNew();
var len = new Regex(@"^.{2,20}$"); //length at least 6 characters and maximum of 20
var digit = new Regex(@"^[0-9]*$"); //a string consisting only of numbers
if (number.StartsWith(@"+") == true) number = number.Substring(1);
number = number.Replace(" ", string.Empty);
try
{
if (!len.IsMatch(Number))
{
NumberContentIsValid = false;
Reason = new Interface("G00005");
}
else if (!digit.IsMatch(Number))
{
NumberContentIsValid = false;
Reason = new Interface("G00005");
}
else if (Region != "IR")
{
NumberContentIsValid = false;
Reason = new Interface("G00005");
}
else
{
var phoneUtil = PhoneNumberUtil.GetInstance();
var mobile = phoneUtil.Parse(Number, Region);
if (phoneUtil.IsValidNumber(mobile) != true)
{
NumberContentIsValid = false;
Reason = new Interface("G00005");
}
else if (phoneUtil.GetNumberType(mobile).ToString() != "MOBILE")
{
NumberContentIsValid = false;
Reason = new Interface("G00005");
}
else
NumberContentIsValid = true;
}
Log.GetLogger().Info(Format.CreateLogMessage("mobile content validation completed successfully.", sw.Elapsed.TotalMilliseconds));
return true;
}
catch (Exception ex)
{
Reason = new Interface("G00001", ex.Message);
Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds));
return false;
}
}
}
如果internationalNumber
和Number
都可用,我想发起Region
。请查看我的convertnumber()
方法:
public void ConvertNumber()
{
var sw = Stopwatch.StartNew();
try
{
if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false)
return;
var phoneUtil = PhoneNumberUtil.GetInstance();
var phone = phoneUtil.Parse(Number, Region);
InternationalNumber = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL);
Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds));
return;
}
catch (Exception ex)
{
Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds));
return;
}
}
为了做到这一点,我使用Number
和Region
属性设置器,如下所示:
public string Region
{
set { region = value.ToUpper(); ConvertNumber(); }
get { return region; }
}
public string Number
{
set { number = value; ConvertNumber(); }
get { return number; }
}
它有效。但它可能会导致意外错误(您会意识到我是否应该查看我的DoesNumberContentValid()
方法。提示:我必须使用number
代替Number
)
现在我想知道有更好的方法吗? (不经意的是,我不能使用班级承包商,因为我无法同时启动这两个参数。)
谢谢。
答案 0 :(得分:1)
也许这对你有用:
public string InternationalNumber
{
set { internationalNumber = value; }
get { return internationalNumber ?? CalcInternationalNumber(); }
}
功能CalcInternationalNumber:
public string CalcInternationalNumber()
{
var sw = Stopwatch.StartNew();
try
{
if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false)
return null;
var phoneUtil = PhoneNumberUtil.GetInstance();
var phone = phoneUtil.Parse(Number, Region);
var intNum = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL);
Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds));
return intNum;
}
catch (Exception ex)
{
Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds));
return null;
}
}
希望这就是你要找的东西。