• 技术文章 >后端开发 >C#.Net教程

    C# 国际手机号类封装 - 使用nuget Global Phone 解析国际手机号

    黄舟黄舟2017-02-28 11:16:33原创1048

    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 解析国际手机号的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:C# 手机号类 nuget
    上一篇:C# 已知经纬度计算两点距离函数 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • C# 动态加载Dll• 从0自学C#04--特性和设计原则• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 【c#教程】C# 属性(Property)• asp.net 图片验证码的HtmlHelper
    1/1

    PHP中文网