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)!