コード例 #1
0
        private ResultMessage ConstructorWithMessageTest(Type exceptionType)
        {
            ResultMessage resultMessage = new ResultMessage(exceptionType);

#pragma warning disable 219
            Exception createdException = null;
#pragma warning restore 219
            try
            {
                // ReSharper disable once RedundantAssignment
                createdException = Activator.CreateInstance(exceptionType, "ExceptionMessage") as Exception;
            }
            catch (Exception exception)
            {
                resultMessage = ResultMessageBuilder.ResultMessageForException(exceptionType, Properties.Resources.FailedToCreateExceptionParameterMessage, exception);
            }

            return(resultMessage);
        }
コード例 #2
0
        /// <summary>
        /// Serializes the and deserialize exception.
        /// </summary>
        /// <typeparam name="T">The type of the exception.</typeparam>
        /// <param name="exceptionToSerialize">The exception to serialize.</param>
        /// <returns>The deserialized exception.</returns>
        public ResultMessage SerializeAndDeserializeException <T>(T exceptionToSerialize) where T : Exception
        {
            ResultMessage resultMessage = new ResultMessage(exceptionToSerialize);

            IFormatter formatter = new BinaryFormatter();

            formatter.Binder = new Binder();

            using (MemoryStream memoryStream = new MemoryStream())
            {
#pragma warning disable 219
                T deserializeException = null;
#pragma warning restore 219
                try
                {
                    formatter.Serialize(memoryStream, exceptionToSerialize);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception exception)
                {
                    return(ResultMessageBuilder.ResultMessageForException(exceptionToSerialize.GetType(), Properties.Resources.FailedToSerializeException, exception));
                }

                try
                {
                    // ReSharper disable once RedundantAssignment
                    deserializeException = (T)formatter.Deserialize(memoryStream);
                }
                catch (Exception exception)
                {
                    return(ResultMessageBuilder.ResultMessageForException(exceptionToSerialize.GetType(), Properties.Resources.FailedToDeserializeException, exception));
                }

                return(resultMessage);
            }
        }