Example #1
0
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            Blob bl = (Blob)value;

            bl.Align();
            if (bl.fOwn == true)
            {
                if (bl.Length == 0L)
                {
                    if (destinationType == typeof(string))
                    {
                        return("");
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            else if (destinationType == typeof(string))
            {
                return(bl.GrabString((IntPtr)0));
            }

            if (ReferenceEquals(destinationType, typeof(InstanceDescriptor)))
            {
                // ' See the next class converter for details on
                // ' InstanceDescriptor conversion

                System.Reflection.ConstructorInfo objC;
                // objC = objT.GetType.GetConstructor(New Type() {GetType(Single), GetType(Single), GetType(Single), GetType(Single), GetType(Ruler.RulerUnits)})
                // Return New InstanceDescriptor(objC, New Object() {objT.Left, objT.Top, objT.Width, objT.Height, objT.Units()})

                objC = bl.GetType().GetConstructor(new Type[] { typeof(byte[]), typeof(Type) });
                return(new InstanceDescriptor(objC, new object[] { (Blob)value, ((Blob)value).Type }));
            }

            if (destinationType.IsEnum == true)
            {
                return(BlobUtil.BytesToEnum(bl, destinationType));
            }
            else if (destinationType.IsArray == true && destinationType.GetElementType().IsEnum == true)
            {
                return(BlobUtil.BytesToEnum(bl, destinationType.GetElementType()));
            }

            if (destinationType.IsClass && (destinationType.BaseType == Blob.Types[(int)BlobTypes.Image] || destinationType == Blob.Types[(int)BlobTypes.Image]))
            {
                return(BytesToImage(bl.GrabBytes()));
            }

            switch (destinationType)
            {
            case var @case when @case == typeof(bool):
            {
                return(bl.get_ByteAt(0L) != 0);
            }

            case var case1 when case1 == typeof(BigInteger):
            {
                return(new BigInteger((byte[])bl));
            }

            case var case2 when case2 == typeof(DateTime):
            {
                return(BytesToDate(bl));
            }

            case var case3 when case3 == typeof(DateTime[]):
            {
                return(BytesToDateArray(bl));
            }

            case var case4 when case4 == typeof(byte[]):
            {
                byte[] a;
                a = new byte[(int)(bl.Length - 1L + 1)];
                Array.Copy((byte[])bl, a, bl.Length);
                return(a);
            }

            case var case5 when case5 == typeof(sbyte[]):
            {
                return(ToSByteArray(bl));
            }

            case var case6 when case6 == typeof(Guid[]):
            case var case7 when case7 == typeof(Guid):
            {
                if (destinationType == typeof(Guid))
                {
                    return(new Guid(bl.GrabBytes((IntPtr)0, 16)));
                }

                int    l = 16;
                int    e = (int)(bl.Length / (double)l);
                int    i;
                int    c = (int)(bl.Length - 1L);
                Guid[] gs;
                gs = new Guid[e];
                e  = 0;
                var loopTo = c;
                for (i = 0; l >= 0 ? i <= loopTo : i >= loopTo; i += l)
                {
                    gs[e] = new Guid(bl.GrabBytes((IntPtr)i, l));
                    e    += 1;
                }

                return(gs);
            }

            case var case8 when case8 == typeof(Color[]):
            case var case9 when case9 == typeof(Color):
            {
                if (destinationType == typeof(Color))
                {
                    Color cc;
                    cc = Color.FromArgb(bl);
                    return(cc);
                }

                int     l = 4;
                int     e = (int)(bl.Length / (double)l);
                int     i;
                int     c = (int)(bl.Length - 1L);
                Color[] cs;
                cs = new Color[e];
                e  = 0;
                var ptr     = bl.DangerousGetHandle();
                var loopTo1 = c;
                for (i = 0; l >= 0 ? i <= loopTo1 : i >= loopTo1; i += l)
                {
                    Native.CopyMemory(ref l, ptr, (IntPtr)4);
                    cs[e] = Color.FromArgb(l);
                    ptr   = ptr + l;
                    e    += 1;
                }

                return(cs);
            }

            case var case10 when case10 == typeof(string):
            {
                if (bl.Length == 0L)
                {
                    return("");
                }
                return(bl.ToString());
            }

            case var case11 when case11 == typeof(decimal[]):
            case var case12 when case12 == typeof(decimal):
            {
                decimal[] d;
                int[]     ints = bl;
                if (Conversions.ToBoolean(ints.Length % 4))
                {
                    throw new ArgumentException("Byte array is not aligned for the Decimal data type.");
                    return(null);
                }

                if (destinationType == typeof(decimal))
                {
                    if (ints.Length != 4)
                    {
                        Array.Resize(ref ints, 4);
                    }
                    return(new decimal(ints));
                }

                var dec = new int[4];
                int e   = bl.Count - 1;
                int i;
                d = new decimal[e + 1];
                var loopTo2 = e;
                for (i = 0; i <= loopTo2; i++)
                {
                    Array.Copy(ints, i, dec, 0, 4);
                    d[i] = new decimal(dec);
                }

                return(d);
            }

            case var case13 when case13 == typeof(double):
            {
                return(BitConverter.ToDouble(bl, 0));
            }

            case var case14 when case14 == typeof(float):
            {
                return(BitConverter.ToSingle(bl, 0));
            }

            case var case15 when case15 == typeof(ulong):
            {
                var u = ToULongArray(new[] { BitConverter.ToInt64(bl, 0) });
                return(u[0]);
            }

            case var case16 when case16 == typeof(long):
            {
                return(BitConverter.ToInt64(bl, 0));
            }

            case var case17 when case17 == typeof(uint):
            {
                var u = ToUIntegerArray(new[] { BitConverter.ToInt32(bl, 0) });
                return(u[0]);
            }

            case var case18 when case18 == typeof(int):
            {
                return(BitConverter.ToInt32(bl, 0));
            }

            case var case19 when case19 == typeof(ushort):
            {
                var u = ToUShortArray(new[] { BitConverter.ToInt16(bl, 0) });
                return(u[0]);
            }

            case var case20 when case20 == typeof(short):
            {
                return(BitConverter.ToInt16(bl, 0));
            }

            case var case21 when case21 == typeof(char):
            {
                return(BitConverter.ToChar(bl, 0));
            }

            case var case22 when case22 == typeof(byte):
            {
                return(bl.get_ByteAt(0L));
            }

            case var case23 when case23 == typeof(sbyte):
            {
                var u = ToSByteArray(bl);
                return(u[0]);
            }

            case var case24 when case24 == typeof(double[]):
            case var case25 when case25 == typeof(float[]):
            case var case26 when case26 == typeof(long[]):
            case var case27 when case27 == typeof(ulong[]):
            case var case28 when case28 == typeof(int[]):
            case var case29 when case29 == typeof(uint[]):
            case var case30 when case30 == typeof(short[]):
            case var case31 when case31 == typeof(ushort[]):
            case var case32 when case32 == typeof(char[]):
            {
                object a = Array.CreateInstance(destinationType.GetElementType(), 1);
                int    l;
                int    e;
                int    f = 0;
                IntPtr ptr;
                byte[] b = bl;
                l = Marshal.SizeOf(a((object)0));
                e = (int)(b.Length / (double)l);
                l = b.Length;
                switch (destinationType.GetElementType())
                {
                case var case33 when case33 == typeof(sbyte):
                {
                    a = Array.CreateInstance(typeof(byte), e);
                    break;
                }

                case var case34 when case34 == typeof(ushort):
                {
                    a = Array.CreateInstance(typeof(short), e);
                    break;
                }

                case var case35 when case35 == typeof(uint):
                {
                    a = Array.CreateInstance(typeof(int), e);
                    break;
                }

                case var case36 when case36 == typeof(ulong):
                {
                    a = Array.CreateInstance(typeof(long), e);
                    break;
                }

                default:
                {
                    a = Array.CreateInstance(destinationType.GetElementType(), e);
                    break;
                }
                }

                ptr = Marshal.AllocCoTaskMem(l);
                Marshal.Copy(b, 0, ptr, l);
                Marshal.Copy(ptr, a, (object)0, e);
                Marshal.FreeCoTaskMem(ptr);
                switch (destinationType.GetElementType())
                {
                case var case37 when case37 == typeof(sbyte):
                {
                    a = ToSByteArray((byte[])a);
                    break;
                }

                case var case38 when case38 == typeof(ushort):
                {
                    a = ToUShortArray((short[])a);
                    break;
                }

                case var case39 when case39 == typeof(uint):
                {
                    a = ToUIntegerArray((int[])a);
                    break;
                }

                case var case40 when case40 == typeof(ulong):
                {
                    a = ToULongArray((long[])a);
                    break;
                }
                }

                return(a);
            }
            }

            if (destinationType.IsValueType)
            {
                object o = null;
                BlobToStructure(bl, ref o);
                return(o);
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }