的兩個成員之後啓動類參數,起初我很抱歉,如果它是一個愚蠢的問題。我是.net新手。在啓動類別
我有一個移動類是這樣的:
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()
方法。tips:我必須用number
而不是Number
) 現在我想知道有沒有更好的方法來做到這一點? (忘記我不能使用班級承包商,因爲我不能同時啓動兩個參數。)
謝謝。
國際號碼屬性總是與其他兩個屬性相關嗎?您確定要允許用戶分別設置國際號碼屬性嗎? –
謝謝親愛的@ Maor Veitsman。是的,國際號碼總是與其他兩個屬性有關,我想單獨分析國際號碼屬性。 – David
可以肯定的是,InternationalNumber屬性應該有一個私人設置者,對吧? –