TagInheritance

DataContractSerializer

DataContractSerializer made its first appearance with release of WCF but can operate outside WCF.

DataContractSerializer can serialize types decorated with DataContract, MessageContract, Serializable attributes, types that implement IXmlSerialisable and all public types without Attributes.

During serialization Reference integrity is by default not preserved. You can preserve it by setting “IsReference = true” to DataContract attribute. Note that this approach my cause problems if DataContractSerializer is not serializer who will deserialize data.

 

You can also use OnSerializing, OnSerialized, OnDeserializing, OnDeserialized Attributes to fine-grain your serialization process.

This tutorial will explain how to serialize and deserialize types by decorating type with DataContract attribute.

First we use need to add reference to System.Runtime.Serialization. Secondly we decorate the type with DataContract Attribute, and inside type wee decorate fields and properties with DataMember Attribute. Then we instantiate DataContractSerializer instance and use its WriteObject and ReadObject methods to serialize/deserialize data.

Example of DataContractSerializer:

By using DataMember Attribute we can serialize automatic properties, because property itself will be serialized and not its backing field.

Because DataContractSerializer does not store type and assembly information we need to tell it which is the type we wish to serialize by specifying type in the DataContractSerializer constructor.

As no type or assembly information are preserved be sure to supply KnownType  Attribute to your base class to be able to use inheritance properly.

DataContractSerializer

Data Contract Serializer

 

You can download solution from this link.

To return to other serialization techniques return to this location.

Inheritance

Inheritance

What exactly is Inheritance? MSDN explanation says : Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

So what can we inherit? For example we have Car and Motorcycle classes, and as we know cars and motorcycles have a few things in common such as engine and color. We could create a base class named Vehicle that has those properties and make Car and Motorcycle classes which will inherit Vehicle class.

 

Can i inherit from multiple classes? No, but you can Inherit multiple Interfaces that inherit one class.

 

I don’t wish for my class or method to be directly instantiated. What can I do about that problem? You can make those classes or methods abstract. Beware, when you make your class abstract one, none of your class members have to be marked as abstract, but if at least one of your members is marked as abstract, entire class must be marked as abstract.

 

Can derived class access private fields of base class? No, it can not. You can only access internal, public, protected, and protected internal members of a base class.

 

Is it possible to prevent our class from becoming other classes base class? Yes, mark that class as sealed and other classes cannot inherit from this class.

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑