jprbest
jprbest

Reputation: 737

"There was an error reflecting type" in C#

I need to serialize and exception, i created a Serializable class

[Serializable]
public class MessageException : Exception
{
    public string Exception
    { get; set; }
    public MessageException()
    { }
    public MessageException(System.Exception ex) 
    {
        this.Exception = ex.Message.ToString();
    }
}

and i try to call the class from the following exception when it occur

catch (Exception ex)
        {
            MessageException exception = new MessageException(ex);
            var exSerializer = new XmlSerializer(typeof(MessageException));


            var writer = new StringWriter();
            exSerializer.Serialize(writer, exception);
            writer.Close();
            Compair2Files(writer.ToString(), BaseString);
        }

it fails on the second row "var exSerializer = new XmlSerializer(typeof(MessageException));"

What I m actually trying to do is when I catch the exception I want to serialized it and then store it into a file for my unit test program. Would you be able to help me thanks in advance Jp

Upvotes: 2

Views: 5563

Answers (1)

SLaks
SLaks

Reputation: 887195

You cannot serialize exceptions using XmlSerializer.
You need to use binary serialization.

The base Exception class is already [Serializale], so your class is useless.
It's also wrong; you're ignoring the stack trace.

Upvotes: 1

Related Questions