What is a DataContract Attribute
DataContract - The flat rate for serializing your objects
In .NET there are many ways to write an object to a file and later load it from it. The data contract offers a simple approach. This has nothing to do with cellular communications, but is a concept introduced by the Windows Communication Foundation (WCF).
So that you can save objects of your class to a file in XML or JSON format you only need the two attributes and. In contrast to other serialization technologies, with data contracts you have to explicitly define which attributes and properties are taken into account during serialization.
You say that your class implements a data contract by defining the attribute on the class. This attribute as well as other relevant attributes and classes are defined in the namespace. A simple example could look like this:
As you can see, you are using the attribute on all properties and attributes that should be taken into account during serialization. For example, if you have a property on a class that only contains current values from a device or a web service, you don't necessarily want to save it.
You can further influence the serialization via the properties of the attribute. If you do not specify anything else, as with the attribute in the previous example, the serialization uses the name of the attribute within the generated file. If you now want to have a name in the generated file that .NET does not allow as a variable name, you can specify this via.
This also allows you to decouple your class interface from the names in the file. If you want to rename the attribute on the class in the example above, you can do this in the source code without having problems reading an existing file.
The property is also used for versioning. With this you indicate that there will be an error during deserialization if the corresponding entry does not exist in the file.
Now you already know the most important attributes to define a data contract for your class. Objects of such a class can be written and read with the. You will find out how this works in another LernMoment. In order not to tease you, I have already included it in the example.
First of all, have fun with your flat rate for saving and loading objects
PS: Even if there are still small gaps, Mono supports this functionality almost completely.
- With data contracts () you can effortlessly save objects of your class in different formats and read them in again.
- The required attributes and classes are defined in the namespace.
- is used in the class definition.
- With you define which properties and attributes of your class should be saved and read.
- In particular, you can use the properties and the attribute to influence the versioning of your file format.
Use the following questions to consolidate what you have learned today:
- What do you use the attribute for?
- What is written to a file if a class is defined but none?
- When should you use the property of the attribute?
The best thing to do is look at the previous questions tomorrow and then again in a few days and answer them without having read the text beforehand.
- You can find the complete source text for today's learning moment here.
- An introduction to data contracts can be found at MSDN. However, this is very much designed for WCF. It is less about simply saving and loading objects.
Did you like this learning moment?
Then register and you will be informed about new learning moments:
- How many countries are under NAM
- Why do I need Genesis Framework
- Mangalore has better infrastructure than Mysore
- What is Darfur
- How is life at the University of Stuttgart
- Could people with webbed feet survive
- What is screaming I'm a great PHP developer
- What is Batman's nightmare
- Why is Layer shutting down
- Rats are better than mice as pets
- What jobs in the advertising agency
- What is UML architecture
- Did the Ottomans legalize homosexuality
- What throws you off
- How do I execute a starting idea
- Is glutamate unhealthy
- What is the cheapest hotel in Rome
- Is there a bank holiday tomorrow
- One simple blow can damage your brain
- What is the Buddha's educational system
- Where can I learn WooCommerce
- Can you start a relationship from scratch
- What does Psalm 17 4 6 mean?
- Which letter is mute in the pseudocode