Getting a byte array from a generic class

Eddie Lascu <[email protected]> Tue, 18 May 2010 10:54:18 -0400
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <8CB0EFF82F198A4B914324DD435718C217DFDA47FA@STO000000XSC0.IBIGROUP.COM>
Hi there,

I need to pick your brains with regard to a more elegant solution to a prob=
lem I'm dealing with right now. I have a generic class that wraps any type =
of numeric values (short, int, decimal, double or float). Removing the part=
s that are not interesting, this class is something like this:

    public abstract class NumericNtcipObject<T> where T : struct, IComparab=
le<T>
    {
        protected NumericNtcipObject(T value)
        {
            double val;
            Debugging.Assert(typeof(T).IsValueType && typeof(T).IsPrimitive=
);
            Debugging.Assert(double.TryParse(value.ToString(), out val));

Value =3D value;
        }

        public T Value
        {
            set
            {
                _value =3D value;
            }
        }

        public byte[] ValueAsByteStream
        {
            get
            {
                  // Need help here
            }
        }

        protected T _value;
    }

The class will be derived from and will only be instantiated with numeric v=
alues. I want to create a property that can give me a byte array containing=
 all the bytes of the underlying value. If this would not be a generic clas=
s, I could do:

return BitConverter.GetBytes(Value);

Unfortunately, BitConverter.GetBytes cannot resolve the type of Value so th=
is solution goes out the window. I need the exact number of bytes, so casti=
ng to a type with a larger range is not good.

I can do something like this:


        public byte[] ValueAsByteStream
        {
            get
            {
                if(typeof(T) =3D=3D typeof(Int16))
                {
                    return BitConverter.GetBytes(Value.ToInt16(new NumberFo=
rmatInfo()));
                }

                if (typeof(T) =3D=3D typeof(Int32))
                {
                    return BitConverter.GetBytes(Value.ToInt32(new NumberFo=
rmatInfo()));
                }

                if (typeof(T) =3D=3D typeof(Int64))
                {
                    return BitConverter.GetBytes(Value.ToInt64(new NumberFo=
rmatInfo()));
                }

                if (typeof(T) =3D=3D typeof(UInt16))
                {
                    return BitConverter.GetBytes(Value.ToUInt16(new NumberF=
ormatInfo()));
                }

                if (typeof(T) =3D=3D typeof(UInt32))
                {
                    return BitConverter.GetBytes(Value.ToUInt32(new NumberF=
ormatInfo()));
                }

                if (typeof(T) =3D=3D typeof(UInt64))
                {
                    return BitConverter.GetBytes(Value.ToUInt64(new NumberF=
ormatInfo()));
                }
                // rest of types here

                return null;
            }
        }

but this is not very nice.

Can either of you smart guys give me a brilliant idea here?

Thanks in advance,
Eddie

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives