Force deserialization with invalid data.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zlf

    #1

    Force deserialization with invalid data.

    I have a class.

    [Serializable]
    public class A
    {
    private int id=0;

    public int Id
    {
    get { return id; }
    set { if (value < 1) { throw ArgumentExcepti on("Id cannot be less
    than 1."); } id = value; }
    }
    }

    And an instance A a = new A().
    If I serialize this instance, then deserialize it back, I will get an
    exception thrown by setter of Id since id is zero. Can u tell me how to make
    deserializer to ignore those exceptions and finish the deserialization ?
    Thanks


  • Chris Dunaway

    #2
    Re: Force deserialization with invalid data.

    On Jul 5, 10:30 pm, "zlf" <z...@hotmail.c omwrote:
    I have a class.
    >
    [Serializable]
    public class A
    {
    private int id=0;
    >
    public int Id
    {
    get { return id; }
    set { if (value < 1) { throw ArgumentExcepti on("Id cannot be less
    than 1."); } id = value; }
    }
    >
    }
    >
    And an instance A a = new A().
    If I serialize this instance, then deserialize it back, I will get an
    exception thrown by setter of Id since id is zero. Can u tell me how to make
    deserializer to ignore those exceptions and finish the deserialization ?
    Thanks
    Why do you initialize your private field to an invalid value to start
    with? If 0 is not a valid value for Id, why do you initialize it to
    that in the first place?

    Chris

    Comment

    Working...