Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Force deserialization with invalid data.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zlf - 06 Jul 2007 04:30 GMT
I have a class.

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

   public int Id
   {
       get { return id; }
       set { if (value < 1) { throw ArgumentException("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
Marc Gravell - 06 Jul 2007 05:04 GMT
> Can u tell me how to make deserializer to ignore those exceptions and finish the deserialization

Under XmlSerlializer? Trick.
One option here is to switch the the DataContractSerializer in .NET
3.0, and mark your entity as [DataContract] rather than
[Serializable]; In WCF you can specify [DataMember] against both
fields and properties, allowing you (via field usage) to bypass the
property setter. If you need to keep the case intact, then
[DataMember(Name="Id")] will continue to serialize it as Id (rather
than id).

Perhaps.

Marc
Chris Dunaway - 06 Jul 2007 15:37 GMT
> I have a class.
>
[quoted text clipped - 17 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.