Пример #1
0
        public static bool TryParse(String candidate, bool allowPartialIP, bool allowName, out ComparableAddress address)
        {
            address = null;
            if (string.IsNullOrEmpty(candidate))
            {
                return(false);
            }

            IPAddress ipAddress;
            PartialIP pip;

            if (IPAddress.TryParse(candidate, out ipAddress))
            {
                address = new ComparableAddress(ipAddress.GetAddressBytes());
                return(true);
            }

            else if (allowPartialIP && PartialIP.TryParse(candidate, out pip))
            {
                address = new ComparableAddress(pip);
                return(true);
            }

            else if (allowName)
            {
                address = new ComparableAddress(candidate);
                return(true);
            }

            return(false);
        }
Пример #2
0
 public override bool Equals(object obj)
 {
     if (obj is IPAddress)
     {
         if (this.IsIP)
         {
             return(obj.Equals(addressIP));
         }
         else if (this.IsPartialIP)
         {
             return(partialIP.Equals(obj));
         }
         else
         {
             return(false);
         }
     }
     else if (obj is ComparableAddress)
     {
         ComparableAddress other = obj as ComparableAddress;
         if (other.IsPartialIP && this.IsIP)
         {
             return(other.partialIP.Equals(this.addressIP));
         }
         else if (other.IsIP && this.IsPartialIP)
         {
             return(this.partialIP.Equals(other.addressIP));
         }
         else
         {
             return(CompareTo(obj) == 0);
         }
     }
     else
     {
         return(false);
     }
 }
Пример #3
0
        public int CompareTo(object obj)
        {
            ComparableAddress other = obj as ComparableAddress;

            if (other == null)
            {
                return(-1);
            }

            if (this.IsIP)
            {
                if (other.IsIP)
                {
                    byte[] thisBytes  = this.addressIP.GetAddressBytes();
                    byte[] otherBytes = other.addressIP.GetAddressBytes();

                    if (thisBytes.Length != otherBytes.Length)  // IPv6 address are deemed to come first
                    {
                        return(otherBytes.Length - thisBytes.Length);
                    }

                    for (int i = 0; i < thisBytes.Length; ++i)
                    {
                        if (thisBytes[i] != otherBytes[i])
                        {
                            return((int)thisBytes[i] - (int)otherBytes[i]);
                        }
                    }

                    return(0);
                }
                else
                {
                    return(-1);
                }
            }

            else if (this.IsPartialIP)
            {
                if (other.IsIP)
                {
                    return(1);
                }
                else if (other.IsPartialIP)
                {
                    return(StringUtility.NaturalCompare(this.partialIP.ToString(), other.partialIP.ToString()));
                }
                else
                {
                    return(-1);
                }
            }

            else
            {
                if (other.IsIP || other.IsPartialIP)
                {
                    return(1);
                }
                else
                {
                    return(addressString.CompareTo(other.addressString));
                }
            }
        }