コード例 #1
0
ファイル: Comparison.cs プロジェクト: orf53975/IoTGateway
        /// <summary>
        /// Tries to make sure <paramref name="x"/> and <paramref name="y"/> have the same type.
        /// </summary>
        /// <param name="x">First value.</param>
        /// <param name="y">Second value.</param>
        /// <returns>If <paramref name="x"/> and <paramref name="y"/> are of the same type after the call.</returns>
        public static bool TryMakeSameType(ref object x, ref object y)
        {
            Type xType = x.GetType();
            Type yType = y.GetType();

            if (xType == yType)
            {
                return(true);
            }

            uint xTypeCode = FilesProvider.GetFieldDataTypeCode(xType);
            uint yTypeCode = FilesProvider.GetFieldDataTypeCode(yType);

            Upgrade(ref x, ref xTypeCode);
            Upgrade(ref y, ref yTypeCode);

            if (xTypeCode == yTypeCode)
            {
                return(true);
            }

            if (yTypeCode == ObjectSerializer.TYPE_STRING)
            {
                x = ToString(x, xTypeCode);
                return(true);
            }

            switch (xTypeCode)
            {
            case ObjectSerializer.TYPE_DECIMAL:
                if (yTypeCode == ObjectSerializer.TYPE_DOUBLE)
                {
                    y = (decimal)((double)y);
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_DOUBLE:
                if (yTypeCode == ObjectSerializer.TYPE_DECIMAL)
                {
                    x = (decimal)((double)x);
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_STRING:
                y = ToString(y, yTypeCode);
                return(true);

            default:
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Serializes an object to a binary destination.
        /// </summary>
        /// <param name="Writer">Binary destination.</param>
        /// <param name="WriteTypeCode">If a type code is to be output.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <param name="Value">The actual object to serialize.</param>
        public override void Serialize(BinarySerializer Writer, bool WriteTypeCode, bool Embedded, object Value)
        {
            if (Value is null)
            {
                if (!WriteTypeCode)
                {
                    throw new NullReferenceException("Value cannot be null.");
                }

                Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
            }
            else
            {
                T[]  Array          = (T[])Value;
                Type LastType       = typeof(T);
                IObjectSerializer S = this.provider.GetObjectSerializer(LastType);
                Type ItemType;
                bool Nullable;

                Writer.WriteBits(ObjectSerializer.TYPE_ARRAY, 6);
                Writer.WriteVariableLengthUInt64((ulong)Array.Length);

                if (Nullable = S.IsNullable)
                {
                    Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
                }
                else
                {
                    Writer.WriteBits(FilesProvider.GetFieldDataTypeCode(LastType), 6);
                }

                foreach (T Item in Array)
                {
                    if (Item == null)
                    {
                        if (Nullable)
                        {
                            Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
                        }
                        else
                        {
                            throw new Exception("Elements cannot be null.");
                        }
                    }
                    else
                    {
                        ItemType = Item.GetType();
                        if (ItemType != LastType)
                        {
                            S        = this.provider.GetObjectSerializer(ItemType);
                            LastType = ItemType;
                        }

                        S.Serialize(Writer, Nullable, true, Item);
                    }
                }
            }
        }
コード例 #3
0
 internal static string ToString(object Value)
 {
     if (Value is null)
     {
         return(string.Empty);
     }
     else
     {
         return(ToString(Value, FilesProvider.GetFieldDataTypeCode(Value.GetType())));
     }
 }
コード例 #4
0
        /// <summary>
        /// Writes an array.
        /// </summary>
        /// <param name="T">Element type.</param>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Writer">Binary writer.</param>
        /// <param name="Value">Value to serialize.</param>
        public static void WriteArray(Type T, FilesProvider Provider, BinarySerializer Writer, Array Value)
        {
            if (Value == null)
            {
                Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
            }
            else
            {
                Type LastType       = T;
                IObjectSerializer S = Provider.GetObjectSerializer(LastType);
                Type ItemType;
                bool Nullable;

                Writer.WriteBits(ObjectSerializer.TYPE_ARRAY, 6);
                Writer.WriteVariableLengthUInt64((ulong)Value.Length);

                if (Nullable = S.IsNullable)
                {
                    Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
                }
                else
                {
                    Writer.WriteBits(FilesProvider.GetFieldDataTypeCode(LastType), 6);
                }

                foreach (object Item in Value)
                {
                    if (Item == null)
                    {
                        Writer.WriteBits(ObjectSerializer.TYPE_NULL, 6);
                    }
                    else
                    {
                        ItemType = Item.GetType();
                        if (ItemType != LastType)
                        {
                            S        = Provider.GetObjectSerializer(ItemType);
                            LastType = ItemType;
                        }

                        S.Serialize(Writer, Nullable, true, Item);
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Class member.
        /// </summary>
        /// <param name="Name">Member name.</param>
        /// <param name="FieldCode">Field Code.</param>
        /// <param name="MemberType">Member type.</param>
        public Member(string Name, ulong FieldCode, Type MemberType)
        {
            this.name           = Name;
            this.fieldCode      = FieldCode;
            this.memberType     = MemberType;
            this.memberTypeInfo = MemberType.GetTypeInfo();

            if (this.memberTypeInfo.IsGenericType)
            {
                Type GT = this.memberType.GetGenericTypeDefinition();
                if (GT == typeof(Nullable <>))
                {
                    this.nullable       = true;
                    this.memberType     = this.memberType.GenericTypeArguments[0];
                    this.memberTypeInfo = this.memberType.GetTypeInfo();
                }
            }

            this.memberFieldDataTypeCode = FilesProvider.GetFieldDataTypeCode(this.memberType);
            this.isNestedObject          = this.memberFieldDataTypeCode == ObjectSerializer.TYPE_OBJECT;

            switch (this.memberFieldDataTypeCode)
            {
            case ObjectSerializer.TYPE_BOOLEAN:
                this.memberTypeCode = TypeCode.Boolean;
                break;

            case ObjectSerializer.TYPE_BYTE:
                this.memberTypeCode = TypeCode.Byte;
                break;

            case ObjectSerializer.TYPE_INT16:
                this.memberTypeCode = TypeCode.Int16;
                break;

            case ObjectSerializer.TYPE_INT32:
                this.memberTypeCode = TypeCode.Int32;
                if (this.memberTypeInfo.IsEnum)
                {
                    this.isEnum   = true;
                    this.hasFlags = true;
                }
                break;

            case ObjectSerializer.TYPE_INT64:
                this.memberTypeCode = TypeCode.Int64;
                break;

            case ObjectSerializer.TYPE_SBYTE:
                this.memberTypeCode = TypeCode.SByte;
                break;

            case ObjectSerializer.TYPE_UINT16:
                this.memberTypeCode = TypeCode.UInt16;
                break;

            case ObjectSerializer.TYPE_UINT32:
                this.memberTypeCode = TypeCode.UInt32;
                break;

            case ObjectSerializer.TYPE_UINT64:
                this.memberTypeCode = TypeCode.UInt64;
                break;

            case ObjectSerializer.TYPE_DECIMAL:
                this.memberTypeCode = TypeCode.Decimal;
                break;

            case ObjectSerializer.TYPE_DOUBLE:
                this.memberTypeCode = TypeCode.Double;
                break;

            case ObjectSerializer.TYPE_SINGLE:
                this.memberTypeCode = TypeCode.Single;
                break;

            case ObjectSerializer.TYPE_DATETIME:
                this.memberTypeCode = TypeCode.DateTime;
                break;

            case ObjectSerializer.TYPE_CHAR:
                this.memberTypeCode = TypeCode.Char;
                break;

            case ObjectSerializer.TYPE_STRING:
                this.memberTypeCode = TypeCode.String;
                break;

            case ObjectSerializer.TYPE_MIN:
            case ObjectSerializer.TYPE_MAX:
            case ObjectSerializer.TYPE_NULL:
                this.memberTypeCode = TypeCode.Empty;
                break;

            case ObjectSerializer.TYPE_ENUM:
                this.memberTypeCode = TypeCode.Object;
                this.isEnum         = true;
                this.hasFlags       = this.memberTypeInfo.IsDefined(typeof(FlagsAttribute), false);
                break;

            case ObjectSerializer.TYPE_OBJECT:
            case ObjectSerializer.TYPE_TIMESPAN:
            case ObjectSerializer.TYPE_BYTEARRAY:
            case ObjectSerializer.TYPE_GUID:
            case ObjectSerializer.TYPE_ARRAY:
            case ObjectSerializer.TYPE_DATETIMEOFFSET:
            default:
                this.memberTypeCode = TypeCode.Object;
                break;
            }
        }
コード例 #6
0
        /// <summary>
        /// Decrements <paramref name="Value"/> to the largest value smaller than <paramref name="Value"/>.
        /// </summary>
        /// <param name="Value">Value</param>
        /// <returns>If operation was successful.</returns>
        public static bool Decrement(ref object Value)
        {
            if (Value is null)
            {
                return(false);
            }

            Type T        = Value.GetType();
            uint TypeCode = FilesProvider.GetFieldDataTypeCode(T);

            switch (TypeCode)
            {
            case ObjectSerializer.TYPE_BOOLEAN:
                if (!(bool)Value)
                {
                    Value = (sbyte)-1;
                }
                else
                {
                    Value = false;
                }

                return(true);

            case ObjectSerializer.TYPE_BYTE:
                byte b = (byte)Value;
                if (b == byte.MinValue)
                {
                    Value = (sbyte)-1;
                }
                else
                {
                    b--;
                    Value = b;
                }
                return(true);

            case ObjectSerializer.TYPE_INT16:
                short i16 = (short)Value;
                if (i16 == short.MinValue)
                {
                    Value = short.MinValue - 1;
                }
                else
                {
                    i16--;
                    Value = i16;
                }
                return(true);

            case ObjectSerializer.TYPE_INT32:
                int i32 = (int)Value;
                if (i32 == int.MinValue)
                {
                    Value = (long)int.MinValue - 1;
                }
                else
                {
                    i32--;
                    Value = i32;
                }
                return(true);

            case ObjectSerializer.TYPE_INT64:
                long i64 = (long)Value;
                if (i64 == long.MinValue)
                {
                    double d = long.MinValue;
                    if (Decrement(ref d))
                    {
                        Value = d;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    i64--;
                    Value = i64;
                }
                return(true);

            case ObjectSerializer.TYPE_SBYTE:
                sbyte i8 = (sbyte)Value;
                if (i8 == sbyte.MinValue)
                {
                    Value = (short)sbyte.MinValue - 1;
                }
                else
                {
                    i8--;
                    Value = i8;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT16:
                ushort ui16 = (ushort)Value;
                if (ui16 == ushort.MinValue)
                {
                    Value = (short)ushort.MinValue - 1;
                }
                else
                {
                    ui16--;
                    Value = ui16;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT32:
                uint ui32 = (uint)Value;
                if (ui32 == uint.MinValue)
                {
                    Value = (int)uint.MinValue - 1;
                }
                else
                {
                    ui32--;
                    Value = ui32;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT64:
                ulong ui64 = (ulong)Value;
                if (ui64 == ulong.MinValue)
                {
                    Value = (long)ulong.MinValue - 1;
                }
                else
                {
                    ui64--;
                    Value = ui64;
                }
                return(true);

            case ObjectSerializer.TYPE_DECIMAL:
                decimal dec = (decimal)Value;
                if (!Decrement(ref dec))
                {
                    return(false);
                }
                else
                {
                    Value = dec;
                    return(true);
                }

            case ObjectSerializer.TYPE_DOUBLE:
                double dbl = (double)Value;
                if (!Decrement(ref dbl))
                {
                    return(false);
                }
                else
                {
                    Value = dbl;
                    return(true);
                }

            case ObjectSerializer.TYPE_SINGLE:
                float sng = (float)Value;
                if (!Decrement(ref sng))
                {
                    return(false);
                }
                else
                {
                    Value = sng;
                    return(true);
                }

            case ObjectSerializer.TYPE_DATETIME:
                DateTime DT = (DateTime)Value;
                if (DT.Ticks == long.MinValue)
                {
                    return(false);
                }
                else
                {
                    Value = new DateTime(DT.Ticks - 1, DT.Kind);
                    return(true);
                }

            case ObjectSerializer.TYPE_DATETIMEOFFSET:
                DateTimeOffset DTO = (DateTimeOffset)Value;
                if (DTO.Ticks == long.MinValue)
                {
                    return(false);
                }
                else
                {
                    Value = new DateTimeOffset(DTO.Ticks - 1, DTO.Offset);
                    return(true);
                }

            case ObjectSerializer.TYPE_TIMESPAN:
                TimeSpan TS = (TimeSpan)Value;
                if (TS.Ticks == long.MinValue)
                {
                    return(false);
                }
                else
                {
                    Value = new TimeSpan(TS.Ticks - 1);
                    return(true);
                }

            case ObjectSerializer.TYPE_CHAR:
                char ch = (char)Value;
                if (ch == char.MinValue)
                {
                    Value = char.MinValue - 1;
                }
                else
                {
                    Value = (char)(ch - 1);
                }

                return(true);

            case ObjectSerializer.TYPE_STRING:
                string s = (string)Value;
                if (Decrement(ref s))
                {
                    Value = s;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_CI_STRING:
                CaseInsensitiveString s2 = (CaseInsensitiveString)Value;
                s = s2.Value;
                if (Decrement(ref s))
                {
                    Value = new CaseInsensitiveString(s);
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_ENUM:
                s = Value.ToString();
                if (Decrement(ref s))
                {
                    Value = s;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_GUID:
                Guid   Guid = (Guid)Value;
                byte[] A    = Guid.ToByteArray();

                A[15]--;
                if (A[15] == 0xff)
                {
                    A[14]--;
                    if (A[14] == 0xff)
                    {
                        A[13]--;
                        if (A[13] == 0xff)
                        {
                            A[12]--;
                            if (A[12] == 0xff)
                            {
                                A[11]--;
                                if (A[11] == 0xff)
                                {
                                    A[10]--;
                                    if (A[10] == 0xff)
                                    {
                                        A[9]--;
                                        if (A[9] == 0xff)
                                        {
                                            A[8]--;
                                            if (A[8] == 0xff)
                                            {
                                                A[6]--;
                                                if (A[6] == 0xff)
                                                {
                                                    A[7]--;
                                                    if (A[7] == 0xff)
                                                    {
                                                        A[4]--;
                                                        if (A[4] == 0xff)
                                                        {
                                                            A[5]--;
                                                            if (A[5] == 0xff)
                                                            {
                                                                A[0]--;
                                                                if (A[0] == 0xff)
                                                                {
                                                                    A[1]--;
                                                                    if (A[1] == 0xff)
                                                                    {
                                                                        A[2]--;
                                                                        if (A[2] == 0xff)
                                                                        {
                                                                            A[3]--;
                                                                            if (A[3] == 0xff)
                                                                            {
                                                                                return(false);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                Value = new Guid(A);
                return(true);

            default:
                return(false);
            }
        }
コード例 #7
0
        /// <summary>
        /// Increments <paramref name="Value"/> to the smallest value greater than <paramref name="Value"/>.
        /// </summary>
        /// <param name="Value">Value</param>
        /// <returns>If operation was successful.</returns>
        public static bool Increment(ref object Value)
        {
            if (Value == null)
            {
                return(false);
            }

            Type T        = Value.GetType();
            uint TypeCode = FilesProvider.GetFieldDataTypeCode(T);

            switch (TypeCode)
            {
            case ObjectSerializer.TYPE_BOOLEAN:
                if ((bool)Value)
                {
                    Value = (byte)2;
                }
                else
                {
                    Value = true;
                }

                return(true);

            case ObjectSerializer.TYPE_BYTE:
                byte b = (byte)Value;
                if (b == byte.MaxValue)
                {
                    Value = (ushort)(byte.MaxValue + 1);
                }
                else
                {
                    b++;
                    Value = b;
                }
                return(true);

            case ObjectSerializer.TYPE_INT16:
                short i16 = (short)Value;
                if (i16 == short.MaxValue)
                {
                    Value = short.MaxValue + 1;
                }
                else
                {
                    i16++;
                    Value = i16;
                }
                return(true);

            case ObjectSerializer.TYPE_INT32:
                int i32 = (int)Value;
                if (i32 == int.MaxValue)
                {
                    Value = (long)int.MaxValue + 1;
                }
                else
                {
                    i32++;
                    Value = i32;
                }
                return(true);

            case ObjectSerializer.TYPE_INT64:
                long i64 = (long)Value;
                if (i64 == long.MaxValue)
                {
                    Value = (ulong)long.MaxValue + 1;
                }
                else
                {
                    i64++;
                    Value = i64;
                }
                return(true);

            case ObjectSerializer.TYPE_SBYTE:
                sbyte i8 = (sbyte)Value;
                if (i8 == sbyte.MaxValue)
                {
                    Value = (short)sbyte.MaxValue + 1;
                }
                else
                {
                    i8++;
                    Value = i8;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT16:
                ushort ui16 = (ushort)Value;
                if (ui16 == ushort.MaxValue)
                {
                    Value = (uint)ushort.MaxValue + 1;
                }
                else
                {
                    ui16++;
                    Value = ui16;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT32:
                uint ui32 = (uint)Value;
                if (ui32 == uint.MaxValue)
                {
                    Value = (ulong)uint.MaxValue + 1;
                }
                else
                {
                    ui32++;
                    Value = ui32;
                }
                return(true);

            case ObjectSerializer.TYPE_UINT64:
                ulong ui64 = (ulong)Value;
                if (ui64 == ulong.MaxValue)
                {
                    double d = ulong.MaxValue;
                    if (Increment(ref d))
                    {
                        Value = d;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    ui64++;
                    Value = ui64;
                }
                return(true);

            case ObjectSerializer.TYPE_DECIMAL:
                decimal dec = (decimal)Value;
                if (!Increment(ref dec))
                {
                    return(false);
                }
                else
                {
                    Value = dec;
                    return(true);
                }

            case ObjectSerializer.TYPE_DOUBLE:
                double dbl = (double)Value;
                if (!Increment(ref dbl))
                {
                    return(false);
                }
                else
                {
                    Value = dbl;
                    return(true);
                }

            case ObjectSerializer.TYPE_SINGLE:
                float sng = (float)Value;
                if (!Increment(ref sng))
                {
                    return(false);
                }
                else
                {
                    Value = sng;
                    return(true);
                }

            case ObjectSerializer.TYPE_DATETIME:
                DateTime DT = (DateTime)Value;
                if (DT.Ticks == long.MaxValue)
                {
                    return(false);
                }
                else
                {
                    Value = new DateTime(DT.Ticks + 1, DT.Kind);
                    return(true);
                }

            case ObjectSerializer.TYPE_TIMESPAN:
                TimeSpan TS = (TimeSpan)Value;
                if (TS.Ticks == long.MaxValue)
                {
                    return(false);
                }
                else
                {
                    Value = new TimeSpan(TS.Ticks + 1);
                    return(true);
                }

            case ObjectSerializer.TYPE_CHAR:
                char ch = (char)Value;
                if (ch == char.MaxValue)
                {
                    Value = char.MaxValue + 1;
                }
                else
                {
                    Value = (char)(ch + 1);
                }
                return(true);

            case ObjectSerializer.TYPE_STRING:
                string s = (string)Value;
                if (Increment(ref s))
                {
                    Value = s;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_ENUM:
                s = Value.ToString();
                if (Increment(ref s))
                {
                    Value = s;
                    return(true);
                }
                else
                {
                    return(false);
                }

            case ObjectSerializer.TYPE_GUID:
                Guid   Guid = (Guid)Value;
                byte[] A    = Guid.ToByteArray();

                A[15]++;
                if (A[15] == 0)
                {
                    A[14]++;
                    if (A[14] == 0)
                    {
                        A[13]++;
                        if (A[13] == 0)
                        {
                            A[12]++;
                            if (A[12] == 0)
                            {
                                A[11]++;
                                if (A[11] == 0)
                                {
                                    A[10]++;
                                    if (A[10] == 0)
                                    {
                                        A[9]++;
                                        if (A[9] == 0)
                                        {
                                            A[8]++;
                                            if (A[8] == 0)
                                            {
                                                A[6]++;
                                                if (A[6] == 0)
                                                {
                                                    A[7]++;
                                                    if (A[7] == 0)
                                                    {
                                                        A[4]++;
                                                        if (A[4] == 0)
                                                        {
                                                            A[5]++;
                                                            if (A[5] == 0)
                                                            {
                                                                A[0]++;
                                                                if (A[0] == 0)
                                                                {
                                                                    A[1]++;
                                                                    if (A[1] == 0)
                                                                    {
                                                                        A[2]++;
                                                                        if (A[2] == 0)
                                                                        {
                                                                            A[3]++;
                                                                            if (A[3] == 0)
                                                                            {
                                                                                return(false);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                Value = new Guid(A);
                return(true);

            default:
                return(false);
            }
        }