/// <summary> /// 解析输入的字符串为一个IPMaskCalculator列表, 格式: IP1,Mask1;IP2,Mask2 /// </summary> /// <param name="IpListString"></param> /// <returns>IPMask列表</returns> public static List <IPMaskCalculator> Parse(string IpListString) { List <IPMaskCalculator> list = new List <IPMaskCalculator>(); string[] IpListArray = IpListString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string iplist in IpListArray) { string[] ip = Split(iplist); IPMaskCalculator imc = null; if (ip.Length == 1) { IPMaskCalculator.TryParse(ip[0], "", out imc); } else if (ip.Length > 1) { IPMaskCalculator.TryParse(ip[0], ip[1], out imc); } if (list != null) { list.Add(imc); } } return(list); }
/// <summary> /// 初始化掩码和比较值, 掩码可以为空, 如果掩码为空, 则自动识别 /// </summary> /// <param name="Mask"></param> /// <param name="IpCompare"></param> /// <returns></returns> public static IPMaskCalculator Parse(string IpCompare, string Mask) { IPAddress ipCompare = IPAddress.Parse(IpCompare); IPMaskCalculator imc = new IPMaskCalculator(); imc.IpBytes = ipCompare.GetAddressBytes(); IPAddress ipMask; if (IPAddress.TryParse(Mask, out ipMask)) { imc.MaskBytes = ipMask.GetAddressBytes(); } else { imc.MaskBytes = new byte[4]; for (int i = 0; i < 4; i++) { if (imc.IpBytes[i] == 0) { imc.MaskBytes[i] = 0; } else { imc.MaskBytes[i] = 255; } } } return(imc); }
/// <summary> /// 初始化掩码和比较值,如果异常则返回false, 如果掩码为空, 则自动识别 /// </summary> /// <param name="Mask"></param> /// <param name="IpCompare"></param> /// <param name="caculater"></param> /// <returns></returns> public static bool TryParse(string IpCompare, string Mask, out IPMaskCalculator caculater) { caculater = null; try { caculater = Parse(IpCompare, Mask); return(true); } catch { return(false); } }
/// <summary> /// 初始化掩码和比较值,如果异常则返回false, 如果掩码为空, 则自动识别 /// </summary> /// <param name="Mask"></param> /// <param name="IpCompare"></param> /// <param name="caculater"></param> /// <returns></returns> public static bool TryParse(string IpCompare, string Mask, out IPMaskCalculator caculater) { caculater = null; try { caculater = Parse(IpCompare, Mask); return true; } catch { return false; } }
/// <summary> /// 初始化掩码和比较值, 掩码可以为空, 如果掩码为空, 则自动识别 /// </summary> /// <param name="Mask"></param> /// <param name="IpCompare"></param> /// <returns></returns> public static IPMaskCalculator Parse(string IpCompare, string Mask) { IPAddress ipCompare = IPAddress.Parse(IpCompare); IPMaskCalculator imc = new IPMaskCalculator(); imc.IpBytes = ipCompare.GetAddressBytes(); IPAddress ipMask; if (IPAddress.TryParse(Mask, out ipMask)) { imc.MaskBytes = ipMask.GetAddressBytes(); } else { imc.MaskBytes = new byte[4]; for (int i = 0; i < 4; i++) { if (imc.IpBytes[i] == 0) imc.MaskBytes[i] = 0; else imc.MaskBytes[i] = 255; } } return imc; }