C# 國際手機號類封裝 - 使用nuget Global Phone 解析國際手機號

來源:互聯網
上載者:User
1. package-install GlobalPhone

2. Phone.cs聽

聽 聽

 public class Phone    {        public string CountryCode { get; set; }        public string InternationalNumber { get; set; }        public string NationalNumber { get; set; }        public string Territory { get; set; }        public bool IsValid { get; set; }        public Phone()        {        }        private Phone(global::GlobalPhone.Number number)            : this()        {            CountryCode = number.CountryCode;            InternationalNumber = number.InternationalString;            NationalNumber = number.NationalFormat;            Territory = number.Territory.Name;            IsValid = number.IsValid;        }        private static bool __libraryLoaded;        private static global::GlobalPhone.Database __databse;        private static void LoadStaticContent()        {            if (!__libraryLoaded)            {                try                {                    var assembly = Assembly.GetExecutingAssembly();                    var dataStream = assembly.GetManifestResourceStream("Common.GlobalPhone.data.json");                    if (dataStream != null)                    {                        var reader = new StreamReader(dataStream);                        var text = reader.ReadToEnd();                        global::GlobalPhone.GlobalPhone.DbText = text;                        __databse = global::GlobalPhone.Database.Load(text);                    }                }                catch (Exception ex)                {                    throw;                }                __libraryLoaded = true;            }        }        private static readonly object __globalPhoneLock = new object();        private static global::GlobalPhone.Region TrnaslateRegion(String countryCode)        {            if (string.IsNullOrEmpty(countryCode))            {                return null;            }            lock (__globalPhoneLock)            {                return __databse.TryGetRegion(countryCode);            }        }        private static global::GlobalPhone.Territory TranslateTerritory(String territoryName)        {            if (string.IsNullOrEmpty(territoryName))            {                return null;            }            lock (__globalPhoneLock)            {                return __databse.TryGetTerritory(territoryName);            }        }        public static Phone TryParseWithCountryCode(string number, string countryCode)        {            LoadStaticContent();            var region = TrnaslateRegion(countryCode);            if (region != null)            {                var territory = region.Territories.FirstOrDefault();                if (territory != null)                {                    return TryParse(number, territory.Name);                }            }            return null;        }        public static Phone TryParse(string number, string territoryName = null)        {            LoadStaticContent();            if (string.IsNullOrEmpty(number))            {                return null;            }            var globalNumber = global::GlobalPhone.GlobalPhone.TryParse(number, territoryName);            if (globalNumber != null && globalNumber.IsValid)            {                return new Phone(globalNumber);            }            //In scenario such as 6597531150 we need to help the library to understand that there is already the country code as a prefix            var territory = TranslateTerritory(territoryName);            if (territory != null)            {                //If the number start with same country code, we will try to add the plus and parse it again                if (number.StartsWith(territory.CountryCode))                {                    globalNumber = global::GlobalPhone.GlobalPhone.TryParse(string.Format("+{0}", number), territoryName);                    if (globalNumber != null && globalNumber.IsValid)                    {                        return new Phone(globalNumber);                    }                }            }            return null;        }    }

以上就是C# 國際手機號類封裝 - 使用nuget Global Phone 解析國際手機號的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.