marshalling BLOBs to types

This is a discussion on marshalling BLOBs to types within the C# Programming forums, part of the General Programming Boards category; i am getting a byte[] array from a blob field in a database, and i need to convert it into ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    marshalling BLOBs to types

    i am getting a byte[] array from a blob field in a database, and i need to convert it into an array of some other primitive type.

    in unmanaged code, this is incredibly easy, but in .NET, it seems painful.

    The Bitconverter is not a great option because there's no generic method and i don't know the types before the code executes.

    any advice would be greatly appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    How are you determining what to turn it into at runtime? Would a switch/case work?
    If you understand what you're doing, you're not learning anything.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i ended up going with a union and reflection.

    Code:
            [StructLayout(LayoutKind.Explicit)]
            private class Union
            {
                [FieldOffset(0)]
                public bool _bool;
                [FieldOffset(0)]
                public byte _byte;
                [FieldOffset(0)]
                public sbyte _sbyte;
                [FieldOffset(0)]
                public char _char;
    
                [FieldOffset(0)]
                public Int16 _Int16;
                [FieldOffset(0)]
                public Int32 _Int32;
                [FieldOffset(0)]
                public Int64 _Int64;
    
                [FieldOffset(0)]
                public UInt16 _UInt16;
                [FieldOffset(0)]
                public UInt32 _UInt32;
                [FieldOffset(0)]
                public UInt64 _UInt64;
    
                [FieldOffset(0)]
                public double _double;
                [FieldOffset(0)]
                public float _float;
    
                [FieldOffset(0)]
                public decimal _decimal;
    
                public static implicit operator double(Union u)
                {
                    return u._double;
                }
    
                public static implicit operator float(Union u)
                {
                    return u._float;
                }
    
                public static implicit operator Int16(Union u)
                {
                    return u._Int16;
                }
                public static implicit operator Int32(Union u)
                {
                    return u._Int32;
                }
                public static implicit operator Int64(Union u)
                {
                    return u._Int64;
                }
    
                public static implicit operator UInt16(Union u)
                {
                    return u._UInt16;
                }
                public static implicit operator UInt32(Union u)
                {
                    return u._UInt32;
                }
                public static implicit operator UInt64(Union u)
                {
                    return u._UInt64;
                }
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-27-2011, 01:37 AM
  2. protocol of marshalling
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 07-11-2008, 01:37 AM
  3. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  4. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  5. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21