Fooker
Fooker

Reputation: 796

How to serialize a complex object and save into file for latter user in c#

I am using a third party dll in which i need to serialize 1 complex object to store in file so that i can deserilize latter. I have tried to serialize by XML serializer but i got the error "There was an error reflecting property 'Notification'". I saw Notification type have not Serializable attribute in the metadata and other types of the complex object have the Serializable attribute.

I don't have control on third party dll, how i can serialize this complex object to save in the file?

Thanks in Advance

Upvotes: 2

Views: 2675

Answers (3)

CurlyPaul
CurlyPaul

Reputation: 1148

Unless you really need to saved object to be in XML, take a look at using the Binary Serializer - http://www.java2s.com/Code/CSharp/File-Stream/BinarySerializer.htm

Any object can be saved and loaded using this, disadvantages are that the file is not human readable and if you need to load it into a non .net app it might get messy.

Upvotes: 0

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

One usually doesn't try to blindly serialize custom objects. Without knowing their internal structure, you're not going to have the best results de-serializing the result.

As an example of a type you could serialize easily:

public class Foo
{
    public string Bar { get; set; }
    public string Baz { get; set; }
    public string Qux { get; set; }
}

You know the internal structure, so you can easily just read and write the properties.

But what about something like DateTime? If you just serialized based on it's public properties, you would have 16 values to write! If we dig in to the internals, we find that DateTime can be represented by a single 64 bit integer. But even then, you're not likely to use that value for serialization. Instead, we typically define a string format that can be understood by both humans and computers alike, perhaps the ISO8601 standard of 2013-06-26T01:23:45. That value is one of many ways it could be serialized, and it isn't any of the public properties.

So if you have a third-party complex object that you want to serialize, think about the following:

  • What data would be needed to completely reconstruct this object?
  • What parts of data does the object expose directly?
  • What parts do I have to calculate or reformat?
  • Is the format I serialize to the best format for all uses of the data, including both storage and transport concerns?
  • Could the data be interpreted differently when deserialized, perhaps based on culture or language details?

Then you might have an idea of how to go about implementing a serializer for this custom object. Often it is simply a matter of looking at one public property instead of all of them, and deserializing by using this value in a constructor.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062550

If you need to use XmlSerializer, one option is XmlAttributeOverrides - this is quite fiddly to use, but allows you to specify how it should be serialized at runtime rather than at compile-time. You do need to cache and re-use the serializer instance, though - otherwise you will leak memory (an entire assembly per serializer instance).

However, a much better option might be (and this is my usual guidance whenever serialization gets remotely tricky): create a separate set of types for serialization - a "DTO layer", if you will. These DTO types should:

  • be simple (data only, no real logic)
  • be structured in the way the serializer wants to work
  • contain any metadata etc that the serializer is interested in
  • have some kind of mapping to/from your actual entity types

Then rather than trying to fight a serializer into working with a hostile type, you just write the trivial code to map between the DTO and domain entities - and the serializer gets to work with a type that it finds convenient.

Other advantages:

  • it forces you to think in terms of "data" rather than "implementation"
  • it allows you to refactor your domain entities without impacting the serialization - it is just the "map between them" code that changes

Upvotes: 1

Related Questions