C # International Mobile phone Number package-use the NuGet global phone to resolve international phone numbers

Source: Internet
Author: User
1. Package-install Globalphone

2. Phone.cs Listening

Listen

 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) {LoadS            Taticcontent ();            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 this 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'll try to add the PL US 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 (glo                    Balnumber);        }                }    } return null; }    }

The above is the C # International mobile phone Number package-use the NuGet global phone to resolve the contents of the International mobile phone number, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.