SerializationAttribute Class

Applies a System.Runtime.Serialization.DataContractAttribute or System.Runtime.Serialization.XmlSerializer, or System.Runtime.Serialization.SoapFormatter to a type.

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.EnumMember, Inherited=false, AllowMultiple=false)]
public sealed class SerializationAttribute : System.Attribute
Usage: This attribute is used to indicate that a type should be serialized using a specific serialization format. It can be applied to various types and members to control how they are processed by serialization engines.
Syntax
public sealed class SerializationAttribute : System.Attribute
Remarks

The SerializationAttribute is a fundamental attribute in the .NET Framework for managing how objects are serialized. It allows developers to explicitly define how a class, struct, enum, interface, field, or property should be represented in a serialized format, such as XML or binary. This is crucial for interoperability, data persistence, and remoting scenarios.

When you apply the SerializationAttribute, you are essentially providing metadata that tells the .NET serialization infrastructure how to handle the marked element. This can involve specifying the data contract name, namespaces, or other serialization-specific configurations.

Fields

The SerializationAttribute class does not expose any public fields.

Constructors

The SerializationAttribute class does not expose any public constructors. It is applied directly to types or members.

Properties
Methods

The SerializationAttribute class inherits methods from System.Attribute, which are standard methods for all attribute types in .NET.

Examples

Here's a simple example of how SerializationAttribute can be used to mark a class for serialization using DataContract serialization:


using System;
using System.Runtime.Serialization;

[DataContract] // This attribute is often used in conjunction or replaced by SerializationAttribute directives
public class Person
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
}

// In a real scenario, SerializationAttribute might be implicitly handled or configured via other attributes
// For demonstration, let's imagine a hypothetical direct usage for a specific serializer:

// [Serialization(SerializerType = typeof(DataContractSerializer))] // Hypothetical direct application
// public class Employee
// {
//     public string EmployeeId { get; set; }
//     public string Department { get; set; }
// }
            
Requirements
Component Version
.NET Framework 2.0 or later
See Also