Beispiel #1
0
        /// <summary>Reads a remote exception from the stream.</summary>
        /// <returns>The remote exception.</returns>
        public RemoteException ReadException()
        {
            Push(InstanceType.Exception);
            Debug.Assert(_current != null);

            // Read the first slice header, and exception's type ID cannot be null.
            string typeId = ReadSliceHeaderIntoCurrent() !;

            ReadIndirectionTableIntoCurrent(); // we read the indirection table immediately

            while (true)
            {
                RemoteException?remoteEx = null;
                Type?           type     = Communicator.ResolveClass(typeId);
                if (type != null)
                {
                    try
                    {
                        remoteEx = (RemoteException?)Activator.CreateInstance(type);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidDataException(
                                  @$ "failed to create an instance of type `{type.Name
                            }' while reading a remote exception with type ID `{typeId}'", ex);
                    }
                }

                // We found the exception.
                if (remoteEx != null)
                {
                    remoteEx.ConvertToUnhandled = true;
                    remoteEx.Read(this);
                    Pop(null);
                    return(remoteEx);
                }

                // Slice off what we don't understand.
                SkipSlice();

                if ((_current.SliceFlags & EncodingDefinitions.SliceFlags.IsLastSlice) != 0)
                {
                    // Create and throw a plain RemoteException with the SlicedData.
                    Debug.Assert(SlicedData != null);
                    remoteEx = new RemoteException(SlicedData.Value);
                    remoteEx.ConvertToUnhandled = true;
                    return(remoteEx);
                }

                typeId = ReadSliceHeaderIntoCurrent() !;
                ReadIndirectionTableIntoCurrent();
            }