Beispiel #1
0
        /// <summary>
        /// The unpack from message.
        /// </summary>
        /// <param name="unpacker">
        /// The unpacker.
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public void UnpackFromMessage(Unpacker unpacker)
        {
            // Read the type name
            this.TypeName = unpacker.LastReadData.AsString();

            // Read the data object as byte array
            byte[] temp;
            unpacker.ReadBinary(out temp);

            // Create a message serializer object
            Type type = Type.GetType(this.TypeName);

            if (type == null)
            {
                type = Assembly.GetCallingAssembly().GetType(this.TypeName);
                if (type == null)
                {
                    throw new ArgumentException(string.Format("Type '{0}' not found.", this.TypeName));
                }
            }

            IMessagePackSingleObjectSerializer ser = BaseMessage.serializers[type];

            // Unpack the message's data object
            this.dataObject = (BaseMessage)ser.UnpackSingleObject(temp);
        }
        /// <summary>
        /// </summary>
        /// <param name="unpacker">
        /// </param>
        public void UnpackFromMessage(Unpacker unpacker)
        {
            // Read the type name
            this.typeName = unpacker.LastReadData.AsString();

            // Read the data object as byte array
            byte[] temp;
            unpacker.ReadBinary(out temp);

            // Create a message serializer object
            IMessagePackSingleObjectSerializer ser = MessagePackSerializer.Create(Type.GetType(this.typeName));

            // Unpack the message's data object
            this.dataObject = (MessageBase)ser.UnpackSingleObject(temp);
        }
Beispiel #3
0
        public static byte[] UnpackBinaryValue(Unpacker unpacker, Type objectType, String memberName)
        {
            try
            {
                byte[] result;
                if (!unpacker.ReadBinary(out result))
                {
                    throw SerializationExceptions.NewFailedToDeserializeMember(objectType, memberName, null);
                }

                return(result);
            }
            catch (MessageTypeException ex)
            {
                throw SerializationExceptions.NewFailedToDeserializeMember(objectType, memberName, ex);
            }
        }
		public static byte[] UnpackBinaryValue( Unpacker unpacker, Type objectType, String memberName )
		{
			try
			{
				byte[] result;
				if ( !unpacker.ReadBinary( out result ) )
				{
					throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, null );
				}

				return result;
			}
			catch ( MessageTypeException ex )
			{
				throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, ex );
			}
		}
		public static byte[] UnpackBinaryValue( Unpacker unpacker, Type objectType, String memberName )
		{
			if ( unpacker == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "unpacker" );
			}

			if ( objectType == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "objectType" );
			}

			if ( memberName == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "memberName" );
			}

#if ASSERT
			Contract.Assert( unpacker != null );
			Contract.Assert( objectType != null );
			Contract.Assert( memberName != null );
#endif // ASSERT

			// ReSharper disable once RedundantAssignment
			var ctx = default( UnpackerTraceContext );
			InitializeUnpackerTrace( unpacker, ref ctx );

			try
			{
				byte[] result;
				if ( !unpacker.ReadBinary( out result ) )
				{
					SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, null );
				}

				Trace( ctx, "ReadDirect", unpacker, memberName );

				return result;
			}
			catch ( MessageTypeException ex )
			{
				SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, ex );
				return default( byte[] ); // never reaches.
			}
		}
Beispiel #6
0
        public static Type Decode(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                throw new SerializationException("Type info must be non-nil array.");
            }

            if (unpacker.ItemsCount != 5)
            {
                throw new SerializationException("Components count of type info is not valid.");
            }

            string compressedTypeName;

            if (!unpacker.ReadString(out compressedTypeName))
            {
                throw new SerializationException("Failed to decode type name component.");
            }

            string assemblySimpleName;

            if (!unpacker.ReadString(out assemblySimpleName))
            {
                throw new SerializationException("Failed to decode assembly name component.");
            }

            byte[] version;
            if (!unpacker.ReadBinary(out version))
            {
                throw new SerializationException("Failed to decode version component.");
            }

            string culture;

            if (!unpacker.ReadString(out culture))
            {
                throw new SerializationException("Failed to decode culture component.");
            }

            byte[] publicKeyToken;
            if (!unpacker.ReadBinary(out publicKeyToken))
            {
                throw new SerializationException("Failed to decode public key token component.");
            }

#if !NETFX_CORE
            var assemblyName =
                new AssemblyName
            {
                Name    = assemblySimpleName,
                Version =
                    new Version(
                        BitConverter.ToInt32(version, 0),
                        BitConverter.ToInt32(version, 4),
                        BitConverter.ToInt32(version, 8),
                        BitConverter.ToInt32(version, 12)
                        ),
                CultureInfo =
                    String.IsNullOrEmpty(culture)
                                                ? null
#if !WINDOWS_PHONE && !SILVERLIGHT
                                                : CultureInfo.GetCultureInfo(culture),
#else
                    : new CultureInfo(culture),
#endif //  !WINDOWS_PHONE
            };
            assemblyName.SetPublicKeyToken(publicKeyToken);
#else
            var assemblyName =
                new AssemblyName(
                    String.Format(
                        CultureInfo.InvariantCulture,
                        "{0},Version={1},Culture={2},PublicKeyToken={3}",
                        assemblySimpleName,
                        new Version(
                            BitConverter.ToInt32(version, 0),
                            BitConverter.ToInt32(version, 4),
                            BitConverter.ToInt32(version, 8),
                            BitConverter.ToInt32(version, 12)
                            ),
                        String.IsNullOrEmpty(culture) ? "neutral" : culture,
                        (publicKeyToken == null || publicKeyToken.Length == 0) ? "null" : Binary.ToHexString(publicKeyToken, false)
                        )
                    );
#endif // !NETFX_CORE

            return
                (Assembly.Load(
                     assemblyName
#if SILVERLIGHT
                     .ToString()
#endif // SILVERLIGHT
                     ).GetType(
                     compressedTypeName.StartsWith(Elipsis, StringComparison.Ordinal)
                                        ? assemblySimpleName + compressedTypeName
                                        : compressedTypeName
#if !NETFX_CORE
                     , throwOnError: true
#endif // !NETFX_CORE
                     ));
        }
		public static Type Decode( Unpacker unpacker )
		{
			if ( !unpacker.IsArrayHeader )
			{
				throw new SerializationException( "Type info must be non-nil array." );
			}

			if ( unpacker.ItemsCount != 5 )
			{
				throw new SerializationException( "Components count of type info is not valid." );
			}

			string compressedTypeName;
			if ( !unpacker.ReadString( out compressedTypeName ) )
			{
				throw new SerializationException( "Failed to decode type name component." );
			}

			string assemblySimpleName;
			if ( !unpacker.ReadString( out assemblySimpleName ) )
			{
				throw new SerializationException( "Failed to decode assembly name component." );
			}

			byte[] version;
			if ( !unpacker.ReadBinary( out version ) )
			{
				throw new SerializationException( "Failed to decode version component." );
			}

			string culture;
			if ( !unpacker.ReadString( out culture ) )
			{
				throw new SerializationException( "Failed to decode culture component." );
			}

			byte[] publicKeyToken;
			if ( !unpacker.ReadBinary( out publicKeyToken ) )
			{
				throw new SerializationException( "Failed to decode public key token component." );
			}

#if !NETFX_CORE
			var assemblyName =
				new AssemblyName
				{
					Name = assemblySimpleName,
					Version =
						new Version(
							BitConverter.ToInt32( version, 0 ),
							BitConverter.ToInt32( version, 4 ),
							BitConverter.ToInt32( version, 8 ),
							BitConverter.ToInt32( version, 12 )
						),
					CultureInfo = 
						String.IsNullOrEmpty( culture ) 
						? null 
#if !WINDOWS_PHONE && !SILVERLIGHT
						: CultureInfo.GetCultureInfo( culture ),
#else
						: new CultureInfo( culture ),
#endif //  !WINDOWS_PHONE
				};
			assemblyName.SetPublicKeyToken( publicKeyToken );
#else
			var assemblyName = 
				new AssemblyName( 
					String.Format( 
						CultureInfo.InvariantCulture, 
						"{0},Version={1},Culture={2},PublicKeyToken={3}",
						assemblySimpleName,
						new Version(
							BitConverter.ToInt32( version, 0 ),
							BitConverter.ToInt32( version, 4 ),
							BitConverter.ToInt32( version, 8 ),
							BitConverter.ToInt32( version, 12 )
						),
						String.IsNullOrEmpty( culture ) ? "neutral" : culture,
						( publicKeyToken == null || publicKeyToken.Length == 0 ) ? "null" : Binary.ToHexString( publicKeyToken, false )
					)
				);
#endif // !NETFX_CORE

			return
				Assembly.Load(
					assemblyName
#if SILVERLIGHT
					.ToString()
#endif // SILVERLIGHT
				).GetType(
					compressedTypeName.StartsWith( Elipsis, StringComparison.Ordinal )
					? assemblySimpleName + compressedTypeName
					: compressedTypeName
#if !NETFX_CORE
					, throwOnError: true
#endif // !NETFX_CORE
				);
		}
	}