Reputation: 796
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
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
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:
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
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:
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:
Upvotes: 1