コード例 #1
0
 public static Enum AsEnum(Type t, object val, bool ignorecase = false, bool checkdisplay = false)
 {
     if (val == null || !EnumEx.IsEnumObject(t))
     {
         return(null);
     }
     if (val is Enum && t.IsInstanceOfType(val))
     {
         return((Enum)val);
     }
     if (!t.IsEnum)
     {
         return(null);
     }
     try {
         string v = val as string;
         if (v != null)
         {
             return(v.AsEnum(t, ignorecase, checkdisplay));
         }
         object n = val;
         if (NumberEx.Numeric(ref n, t.NumericType(), false))
         {
             return(Enum.ToObject(t, n) as Enum);
         }
     } catch { }
     return(null);
 }
コード例 #2
0
 /// <summary>
 /// Ensure that all provided flags are on or off
 /// </summary>
 /// <param name="flag">Set of values to modify</param>
 /// <param name="on">True to turn on and False to clear</param>
 /// <returns></returns>
 protected bool Flag(BigBitBlob flag, bool on)
 {
     if (Max != flag?.Max)
     {
         return(false);
     }
     Blob = (on) ?
            NumberEx.Or(Blob, flag.Blob, false) :
            NumberEx.And(Blob, NumberEx.Complement(flag.Blob, true));
     return(true);
 }
コード例 #3
0
        public static Enum AsEnum(this string s, Type t, bool ignorecase = false, bool checkdisplay = false)
        {
            if (t == null || string.IsNullOrWhiteSpace(s))
            {
                return(null);
            }
            try {
                object n = s;
                if (NumberEx.Numeric(ref n, t.NumericType(), errdef: false, parse: true))
                {
                    return(Enum.ToObject(t, n) as Enum);
                }
                StringComparison comp = ignorecase ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture;

                Enum ret = string.Equals(s, StringEx.SafeDBName(s), comp) ? Get(t)?.FirstOrDefault(z => string.Equals(z?.ToString(), s, comp)) : null;
                if (ret != null)
                {
                    return(ret);
                }
            } catch { }
            return(null);
        }
コード例 #4
0
        public static TimeSpan?GetTime(object val, bool parse)
        {
            if (val is TimeSpan? && IOEx.IsNullable(val))
            {
                return((TimeSpan?)val);
            }
            if (val is TimeSpan)
            {
                return((TimeSpan)val);
            }
            if (val is long)
            {
                return(new TimeSpan((long)val));
            }
            long?n = NumberEx.GetLong(val);

            if (n != null)
            {
                return((n == 0) ? TimeSpan.Zero : new TimeSpan(n.Value));
            }
            string s = parse ? val?.ToString() : null;

            if (s == null)
            {
                return((TimeSpan?)null);
            }
            TimeSpan ret;

            if (IsTimeSpan(s) && TryFromServer(s, out ret, TimeSpan.Zero))
            {
                return(ret);
            }
            if (TimeSpan.TryParse(s, out ret))
            {
                return(ret);
            }
            return(ParseTime(s));
        }
コード例 #5
0
        public static TimeSpan?ParseTime(string val)
        {
            if (String.IsNullOrWhiteSpace(val))
            {
                return(null);
            }
            string[]       v    = val.Split(StringEx.WhiteSpaceSplit, StringSplitOptions.RemoveEmptyEntries);
            double         n    = 0;
            bool           nok  = v.Length == 2 && NumberEx.TryFromServer(v[0], out n, 0.0);
            int            span = (int)Math.Floor(n);
            DateTimeOffset now  = DateTimeOffset.Now;
            string         ts   = v[v.Length - 1].ToLower();

            switch (ts)
            {
            case "past": case "min": return(TimeSpan.MinValue);

            case "future": case "max": return(TimeSpan.MaxValue);

            case "now":
            case "zero":
            case "0": return(TimeSpan.Zero);

            case "millisecond":
            case "milliseconds": return(new TimeSpan(0, 0, 0, 0, span));

            case "second":
            case "seconds": return(new TimeSpan(0, 0, span));

            case "minute":
            case "minutes": return(new TimeSpan(0, span, 0));

            case "hour":
            case "hours": return(new TimeSpan(span, 0, 0));

            case "day":
            case "days": return(new TimeSpan(span, 0, 0, 0));

            case "week":
            case "weeks": return(new TimeSpan(span * 7, 0, 0, 0));

            case "month":
            case "months": return(new TimeSpan((span * 30) + (int)Math.Floor(span * 10.5 / 24.0), (int)Math.Floor((span * 10.5) % 24.0), (int)Math.Floor((((span * 10.5) % 24.0) % 1) * 60), 0));                  // 30 days, 10.5 hours per month = 365.25 days

            case "year":
            case "years": return(new TimeSpan((span * 365) + (int)Math.Floor(span / 4.0), (int)Math.Floor((span % 4.0) * 6), 0, 0));                 // 365.25 days in a year factoring in leap year

            default:
                if (String.Compare(StringEx.Localize("past"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "past";
                }
                if (String.Compare(StringEx.Localize("min"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "min";
                }
                if (String.Compare(StringEx.Localize("future"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "future";
                }
                if (String.Compare(StringEx.Localize("max"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "max";
                }
                if (String.Compare(StringEx.Localize("now"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "now";
                }
                if (String.Compare(StringEx.Localize("zero"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "zero";
                }
                if (String.Compare(StringEx.Localize("millisecond"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "millisecond";
                }
                if (String.Compare(StringEx.Localize("milliseconds"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "milliseconds";
                }
                if (String.Compare(StringEx.Localize("minute"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "minute";
                }
                if (String.Compare(StringEx.Localize("hour"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "hour";
                }
                if (String.Compare(StringEx.Localize("day"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "day";
                }
                if (String.Compare(StringEx.Localize("week"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "week";
                }
                if (String.Compare(StringEx.Localize("weeks"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "weeks";
                }
                if (String.Compare(StringEx.Localize("month"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "month";
                }
                if (String.Compare(StringEx.Localize("months"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "months";
                }
                if (String.Compare(StringEx.Localize("year"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "year";
                }
                if (String.Compare(StringEx.Localize("years"), ts, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    goto case "years";
                }
                return(null);
            }
        }
コード例 #6
0
        internal static object FromString(object obj, Type t, string fmt, bool usedef, bool tryconvert)
        {
            if (t == null)
            {
                return(obj);
            }
            try {
                if (EnumEx.IsEnumObject(t))
                {
                    return(EnumEx.AsEnum(t, obj));
                }
                string s = obj as string;
                switch (Type.GetTypeCode(t))
                {
                case TypeCode.Boolean: return(NumberEx.GetBool(obj) ?? ((usedef) ? false : (object)null));

                case TypeCode.SByte: return(NumberEx.GetSByte(obj) ?? ((usedef) ? (sbyte)0 : (object)null));

                case TypeCode.Byte: return(NumberEx.GetByte(obj) ?? ((usedef) ? (byte)0 : (object)null));

                case TypeCode.Int16: return(NumberEx.GetShort(obj) ?? ((usedef) ? (short)0 : (object)null));

                case TypeCode.UInt16: return(NumberEx.GetUShort(obj) ?? ((usedef) ? (ushort)0 : (object)null));

                case TypeCode.Int32:
                    int?n = NumberEx.GetInt(obj) ?? ((usedef) ? (int)0 : (int?)null);
                    if (typeof(int[]).IsAssignableFrom(t))
                    {
                        return(n?.AsArray());
                    }
                    return(n);

                case TypeCode.UInt32: return(NumberEx.GetUInt(obj) ?? ((usedef) ? (uint)0 : (object)null));

                case TypeCode.Int64: return(NumberEx.GetLong(obj) ?? ((usedef) ? (long)0 : (object)null));

                case TypeCode.UInt64: return(NumberEx.GetULong(obj) ?? ((usedef) ? (ulong)0 : (object)null));

                case TypeCode.Single: return(NumberEx.GetFloat(obj) ?? ((obj is string && String.IsNullOrWhiteSpace((string)obj)) ? float.NaN : (usedef) ? (float)0 : (object)null));

                case TypeCode.Double: return(NumberEx.Get(obj) ?? ((obj is string && String.IsNullOrWhiteSpace((string)obj)) ? double.NaN : (usedef) ? (double)0 : (object)null));

                case TypeCode.Decimal: return(NumberEx.Get(obj) ?? ((usedef) ? decimal.Zero : (object)null));

                case TypeCode.String: return((obj == null) ? (string)null : (obj as string) ?? obj.ToString());

                case TypeCode.DateTime:
                    DateTimeOffset?dt = DateTimeEx.GetDate(obj);
                    if (typeof(DateTimeOffset?).IsAssignableFrom(t))
                    {
                        return(dt);
                    }
                    if (typeof(DateTimeOffset).IsAssignableFrom(t))
                    {
                        return((dt != null) ? dt.Value : (usedef) ? DateTimeOffset.Now : (object)null);
                    }
                    if (typeof(DateTime?).IsAssignableFrom(t))
                    {
                        return((dt != null) ? dt.Value.DateTime : (usedef) ? (DateTime?)null : (object)null);
                    }
                    if (typeof(DateTime).IsAssignableFrom(t))
                    {
                        return((dt != null) ? dt.Value.DateTime : (usedef) ? DateTime.Now : (object)null);
                    }
                    return((dt != null) ? (object)dt.Value : null);

                case TypeCode.Object:
                    if (typeof(DateTimeOffset?).IsAssignableFrom(t) ||
                        typeof(DateTimeOffset).IsAssignableFrom(t) ||
                        typeof(DateTime?).IsAssignableFrom(t) ||
                        typeof(DateTime).IsAssignableFrom(t))
                    {
                        goto case TypeCode.DateTime;
                    }
                    if (typeof(TimeSpan?).IsAssignableFrom(t) ||
                        typeof(TimeSpan).IsAssignableFrom(t))
                    {
                        TimeSpan?ts = DateTimeEx.GetTime(obj);
                        if (typeof(TimeSpan?).IsAssignableFrom(t))
                        {
                            return((ts != null) ? ts.Value : (usedef) ? TimeSpan.Zero : (object)null);
                        }
                        if (typeof(TimeSpan).IsAssignableFrom(t))
                        {
                            return((ts != null) ? ts.Value : (usedef) ? TimeSpan.Zero : (object)null);
                        }
                        return((ts != null) ? (object)ts.Value : null);
                    }
                    if (t.IsArray)
                    {
                        Type arrt = t.GetElementType();
                        if (s != null)
                        {
                            string[] arr = Split(s, ",", true);
                            if (arr != null)
                            {
                                int   len  = arr.Length;
                                Array many = Array.CreateInstance(arrt, len);
                                for (int i = 0; i < len; i++)
                                {
                                    many.SetValue(FromString(arr[i], arrt, fmt, usedef), i);
                                }
                                return(many);
                            }
                        }
                        else
                        {
                            object one = FromString(obj, arrt, fmt, usedef);
                            if (one != null)
                            {
                                return(one.AsArray());
                            }
                        }
                    }
                    break;
                }
            } catch { }
            return((obj != null && t.IsInstanceOfType(obj)) ? obj : (usedef && t.IsValueType) ? Activator.CreateInstance(t) : null);
        }
コード例 #7
0
 public static BigBitBlob operator &(BigBitBlob a, BigBitBlob b)
 {
     return((a == null || b == null) ? null : new BigBitBlob(Math.Max(a.Max, b.Max), false, NumberEx.And(a.Blob, b.Blob, true)));
 }
コード例 #8
0
 public static BigBitBlob operator |(BigBitBlob a, BigBitBlob b)
 {
     return((a == null && b == null) ? null : (a == null) ? b.Dupe() : (b == null) ? a.Dupe() : new BigBitBlob(Math.Max(a.Max, b.Max), false, NumberEx.Or(a.Blob, b.Blob, true)));
 }
コード例 #9
0
 public static BigBitBlob operator ~(BigBitBlob a)
 {
     return((a == null) ? null : new BigBitBlob(a.Max, false, NumberEx.Complement(a.Blob, true)));
 }