Exemplo n.º 1
0
        public static async Task <Type> DecodeRuntimeTypeInfoAsync(Unpacker unpacker, Func <PolymorphicTypeVerificationContext, bool> typeVerifier, CancellationToken cancellationToken)
        {
            CheckUnpackerForRuntimeTypeInfoDecoding(unpacker);

            using (var subTreeUnpacker = unpacker.ReadSubtree())
            {
                var encodeType = await subTreeUnpacker.ReadByteAsync(cancellationToken).ConfigureAwait(false);

                if (!encodeType.Success)
                {
                    ThrowFailedToDecodeEncodingType();
                }

                if (encodeType.Value != ( byte )TypeInfoEncoding.RawCompressed)
                {
                    ThrowUnknownEncodingType(encodeType.Value);
                }

                var compressedTypeName = await subTreeUnpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false);

                if (!compressedTypeName.Success)
                {
                    ThrowFailedToDecodeCompressedTypeName();
                }

                var assemblySimpleName = await subTreeUnpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false);

                if (!assemblySimpleName.Success)
                {
                    ThrowFailedToDecodeAssemblySimpleName();
                }

                var version = await subTreeUnpacker.ReadBinaryAsync(cancellationToken).ConfigureAwait(false);

                if (!version.Success)
                {
                    ThrowFailedToDecodeAssemblyVersion();
                }

                var culture = await subTreeUnpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false);

                if (!culture.Success)
                {
                    ThrowFailedToDecodeAssemblyCulture();
                }

                var publicKeyToken = await subTreeUnpacker.ReadBinaryAsync(cancellationToken).ConfigureAwait(false);

                if (!publicKeyToken.Success)
                {
                    ThrowFailedToDecodeAssemblyKeyToken();
                }

                var assemblyName = BuildAssemblyName(assemblySimpleName.Value, version.Value, culture.Value, publicKeyToken.Value);
                var typeFullName = DecompressTypeName(assemblyName.Name, compressedTypeName.Value);
                RuntimeTypeVerifier.Verify(assemblyName, typeFullName, typeVerifier);
                return(LoadDecodedType(assemblyName, typeFullName));
            }
        }
Exemplo n.º 2
0
        public static Type DecodeRuntimeTypeInfo(Unpacker unpacker, Func <PolymorphicTypeVerificationContext, bool> typeVerifier)
        {
            CheckUnpackerForRuntimeTypeInfoDecoding(unpacker);

            using (var subTreeUnpacker = unpacker.ReadSubtree())
            {
                byte encodeType;
                if (!subTreeUnpacker.ReadByte(out encodeType))
                {
                    ThrowFailedToDecodeEncodingType();
                }

                if (encodeType != ( byte )TypeInfoEncoding.RawCompressed)
                {
                    ThrowUnknownEncodingType(encodeType);
                }

                string compressedTypeName;
                if (!subTreeUnpacker.ReadString(out compressedTypeName))
                {
                    ThrowFailedToDecodeCompressedTypeName();
                }

                string assemblySimpleName;
                if (!subTreeUnpacker.ReadString(out assemblySimpleName))
                {
                    ThrowFailedToDecodeAssemblySimpleName();
                }

                byte[] version;
                if (!subTreeUnpacker.ReadBinary(out version))
                {
                    ThrowFailedToDecodeAssemblyVersion();
                }

                string culture;
                if (!subTreeUnpacker.ReadString(out culture))
                {
                    ThrowFailedToDecodeAssemblyCulture();
                }

                byte[] publicKeyToken;
                if (!subTreeUnpacker.ReadBinary(out publicKeyToken))
                {
                    ThrowFailedToDecodeAssemblyKeyToken();
                }

                var assemblyName = BuildAssemblyName(assemblySimpleName, version, culture, publicKeyToken);
                var typeFullName = DecompressTypeName(assemblyName.Name, compressedTypeName);
                RuntimeTypeVerifier.Verify(assemblyName, typeFullName, typeVerifier);

                return(LoadDecodedType(assemblyName, typeFullName));
            }
        }