Пример #1
0
        public static byte[] SerializeToBytes(object obj, int serializationType)
        {
            IProtocolFormatter formatter;
            switch (serializationType)
            {
                case AMF0:
                    formatter = new AmfFormatter();
                    break;

#if (!SILVERLIGHT && !PURE_CLIENT_LIB && !WINDOWS_PHONE8)
                case WOLF:
                    formatter = new WolfFormatter();
                    break;
#endif
                case AMF3:
                default:
                    formatter = new AmfV3Formatter();
                    break;
            }
            
            MessageWriter.writeObject(obj, formatter);
            ProtocolBytes protocolBytes = formatter.GetBytes();
            formatter.Cleanup();

            return protocolBytes.bytes;
        }
Пример #2
0
        public static byte[] ToBytes( Object obj, int type )
        {
            IProtocolFormatter formatter = null;

            switch( type )
            {
                case AMF0:
                    formatter = new AmfFormatter();
                    break;

                case AMF3:
                    formatter = new AmfV3Formatter();
                    break;
#if( !SILVERLIGHT && !PURE_CLIENT_LIB && !WINDOWS_PHONE8)
                case WOLF:
                    formatter = new WolfFormatter();
                    break;
#endif
                default:
                    throw new Exception( "Unknown protocol type" );
            }

            MessageWriter.writeObject( obj, formatter );
            ProtocolBytes bytes = formatter.GetBytes();
            formatter.Cleanup();

            if( bytes.bytes.Length != bytes.length )
            {
                byte[] result = new byte[ bytes.length ];
                Array.Copy( bytes.bytes, result, bytes.length );
                return result;
            }
            else
            {
                return bytes.bytes;
            }
        }