예제 #1
0
 public static string HashHex(EHashType type, Stream bs, bool seekBegin)
 {
     return(HexHelper.Buffer2Hex(HashRaw(type, bs, seekBegin)));
 }
예제 #2
0
 public static string HashHex(EHashType type, byte[] bs, int index, int length)
 {
     return(HexHelper.Buffer2Hex(HashRaw(type, bs, index, length)));
 }
예제 #3
0
 public static string HashHex(EHashType type, byte[] bs)
 {
     return(HexHelper.Buffer2Hex(HashRaw(type, bs)));
 }
예제 #4
0
 public HashHelper(EHashType type, byte[] bx)
 {
     _Raw = HashRaw(type, bx, 0, bx.Length);
     _Hex = HexHelper.Buffer2Hex(_Raw);
 }
예제 #5
0
        public static object ConvertTo(string input, Type type)
        {
            if (!string.IsNullOrEmpty(input))
            {
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    type = type.GenericTypeArguments[0];
                }
                if (type.IsEnum)
                {
                    string[] f = Enum.GetNames(type);

                    long   iret = 0;
                    object ret  = null;
                    foreach (string en in input.Split(GetSplitChar(type)))
                    {
                        foreach (string v in f)
                        {
                            if (string.Compare(v, en, true) == 0)
                            {
                                object v1 = Enum.Parse(type, v);
                                if (v1 != null)
                                {
                                    if (ret == null)
                                    {
                                        ret  = v1;
                                        iret = Convert.ToInt64(v1);
                                    }
                                    else
                                    {
                                        // TODO Convert multienum One|Two
                                        iret |= Convert.ToInt64(v1);
                                    }
                                }
                            }
                        }
                    }

                    return(ret);
                }

                // String conversion
                if (type == _StringType)
                {
                    return(input);
                }
                if (input.Trim().ToLowerInvariant() == "null")
                {
                    return(null);
                }

                // Bool
                if (type == _BoolType)
                {
                    Boolean r;
                    if (!Boolean.TryParse(input.Trim(), out r))
                    {
                        string si = input.ToLowerInvariant();
                        return(si == "y" || si == "s" || si == "yes" || si == "si" || si == "1" || si == "true");
                    }
                    return(r);
                }
                // Date/Time
                if (type == _TimeSpanType)
                {
                    TimeSpan r;
                    if (!TimeSpan.TryParse(input.Trim(), CultureInfo.InvariantCulture, out r))
                    {
                        long ms = Convert.ToInt64(MathHelper.Calc(input));
                        return(TimeSpan.FromMilliseconds(ms));
                    }
                    return(r);
                }
                if (type == _DateTimeType)
                {
                    DateTime r;
                    if (!DateTime.TryParse(input.Trim(), CultureInfo.InvariantCulture, DateTimeStyles.None, out r))
                    {
                        return(DateTime.MinValue);
                    }
                    return(r);
                }

                // Numeric Conversion
                if (type == _ByteType)
                {
                    byte r;
                    if (!byte.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToByte(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _SByteType)
                {
                    sbyte r;
                    if (!sbyte.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToSByte(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _Int64Type)
                {
                    long r;
                    if (!long.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToInt64(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _UInt64Type)
                {
                    ulong r;
                    if (!ulong.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToUInt64(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _Int32Type)
                {
                    int r;
                    if (!int.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToInt32(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _UInt32Type)
                {
                    uint r;
                    if (!uint.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToUInt32(MathHelper.Calc(input)));
                    }
                    return(r);
                }

                if (type == _Int16Type)
                {
                    short r;
                    if (!short.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToInt16(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _UInt16Type)
                {
                    ushort r;
                    if (!ushort.TryParse(input.Trim(), out r))
                    {
                        return(Convert.ToUInt16(MathHelper.Calc(input)));
                    }
                    return(r);
                }

                // Decimal (With '.' as decimal separator)
                if (type == _DoubleType)
                {
                    double r;
                    if (!double.TryParse(input.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out r))
                    {
                        return(Convert.ToDouble(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _DecimalType)
                {
                    Decimal r;
                    if (!Decimal.TryParse(input.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out r))
                    {
                        return(Convert.ToDecimal(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _FloatType)
                {
                    float r;
                    if (!float.TryParse(input.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out r))
                    {
                        return(Convert.ToSingle(MathHelper.Calc(input)));
                    }
                    return(r);
                }
                if (type == _IPAddressType)
                {
                    ushort    prto = 0;
                    IPAddress ip;
                    if (IPHelper.ParseIpPort(input, out ip, ref prto))
                    {
                        return(ip);
                    }
                    return(IPAddress.Any);
                }
                if (type == _FileInfoType)
                {
                    return(new FileInfo(input));
                }
                if (type == _DirectoryInfoType)
                {
                    return(new DirectoryInfo(input));
                }

                if (type == _IPEndPointType)
                {
                    IPAddress ip;
                    ushort    port = 0;
                    if (!IPHelper.ParseIpPort(input, out ip, ref port))
                    {
                        return(null);
                    }
                    if (port == 0)
                    {
                        return(null);
                    }
                    return(new IPEndPoint(ip, port));
                }
                if (type == _ByteArrayType)
                {
                    return(HexHelper.FromHexString(input));
                }

                // Array
                if (type.IsArray)
                {
                    List <object> l  = new List <object>();
                    Type          gt = type.GetElementType();
                    foreach (string ii in input.Split(GetSplitChar(gt)))
                    {
                        object ov = ConvertTo(ii, gt);
                        if (ov == null)
                        {
                            continue;
                        }

                        l.Add(ov);
                    }

                    Array a = (Array)Activator.CreateInstance(type, l.Count);
                    Array.Copy(l.ToArray(), a, l.Count);
                    return(a);
                }

                // List
                if (_IListType.IsAssignableFrom(type))
                {
                    IList l = (IList)Activator.CreateInstance(type);
                    // If dosent have T return null
                    if (type.GenericTypeArguments == null || type.GenericTypeArguments.Length == 0)
                    {
                        return(null);
                    }

                    Type gt = type.GenericTypeArguments[0];
                    foreach (string ii in input.Split(GetSplitChar(gt)))
                    {
                        object ov = ConvertTo(ii, gt);
                        if (ov == null)
                        {
                            continue;
                        }

                        l.Add(ov);
                    }
                    return(l);
                }

                // Objects
                if (type.IsClass)
                {
                    // Is Convertible (Like Password)
                    TypeConverter conv = TypeDescriptor.GetConverter(type);
                    if (conv.CanConvertFrom(_StringType))
                    {
                        return(conv.ConvertFrom(input));
                    }
                    ;

                    if (input.StartsWith("{") && input.EndsWith("}"))
                    {
                        input = input.Substring(1, input.Length - 2);
                    }

                    return(ArgumentHelper.Parse(type, input));
                }
            }

            return(null);
        }