Overview
The NetDataContractAttribute class indicates that a type is known to the NetDataContractSerializer. This attribute is used to serialize and deserialize data contracts. When you apply this attribute to a class, struct, enum, interface, or delegate, you are explicitly telling the NetDataContractSerializer to include the type information, such as the namespace and name, during serialization. This is particularly useful when you need to ensure that the serialized data is strongly typed and can be deserialized correctly even if the deserializing assembly doesn't have direct knowledge of the type.
When to Use
Use NetDataContractAttribute in the following scenarios:
- When you need to serialize and deserialize types across different application domains or assemblies where the type definitions might not be readily available.
- To ensure that the type name and namespace are preserved during serialization, allowing for robust deserialization.
- When working with WCF (Windows Communication Foundation) services that rely on
NetDataContractSerializerfor efficient and strongly-typed data exchange. - To explicitly mark types that are intended for data contract serialization, even if they don't strictly require it by default.
Syntax
The NetDataContractAttribute is applied directly to the type definition.
[System.Runtime.Serialization.NetDataContractAttribute()]
public class MyDataContractType
{
// ... members ...
}
In most cases, you can use the shortened form:
[NetDataContract]
public class MyDataContractType
{
// ... members ...
}
Remarks
The NetDataContractSerializer uses the NetDataContractAttribute to determine whether to include the full type information (namespace and name) in the serialized output. If a type is marked with this attribute, the serializer will emit the clrtype element in the XML output, specifying the fully qualified name of the type. This makes the serialized data self-describing and allows for deserialization without requiring a direct reference to the type's assembly.
Conversely, if the NetDataContractAttribute is not present, and the serializer can infer the type from the context (e.g., if the deserializing assembly has a direct reference to the type), it may omit the explicit type information to reduce the size of the serialized data. This is the default behavior for serializers like DataContractSerializer when type inference is possible.
The NetDataContractAttribute is AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false, AllowMultiple = false). This means it can be applied to classes, structs, enums, interfaces, and delegates. It is not inherited by derived classes and can only be applied once to a given type.
Example
Consider the following example demonstrating the usage of NetDataContractAttribute:
using System;
using System.Runtime.Serialization;
[NetDataContract]
public class UserProfile
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
var profile = new UserProfile { Name = "Alice", Age = 30 };
var serializer = new NetDataContractSerializer();
// Serialize to a string (for demonstration)
using (var writer = new System.IO.StringWriter())
{
serializer.WriteObject(writer, profile);
Console.WriteLine(writer.ToString());
}
// Output will include type information like:
//
// Alice
// 30
//
}
}