Open sandboxFocusImprove this doc

SR0007: Serializer of the Base Type Has Not Been Generated

Severity Error
Reported by PostSharp
Message Cannot apply [PSerializable] to type '<Type>' because the serializer of its base type has not been generated, although the base type is [PSerializable]. Make sure that PostSharp processes the project that defines the base type.

Cause

[PSerializableAttribute] generates a serializer for <Type> that extends the serializer of its base type. When the base type is [PSerializableAttribute], PostSharp generates the serializer of the base type when it builds the assembly that defines the base type, as a nested class named Serializer.

PostSharp reports SR0007 when the base type has the [PSerializableAttribute] attribute but no serializer. This happens when the base type is in another assembly that was built without PostSharp: the project of this assembly references the PostSharp.Redist package, which contains [PSerializableAttribute], but not the PostSharp package, which contains the compiler, or it sets the SkipPostSharp MSBuild property to True.

In the following example, Entity is defined in a library built without PostSharp, and Customer derives from it.

using PostSharp.Serialization;

namespace Errors.SR0007_Error.Domain;

// This library is built without PostSharp, so [PSerializable] does not generate the serializer of Entity.
[PSerializable]
public class Entity
{
    public int Id { get; set; }
}
// @SkipPostSharpOnDependency

using System;
using System.IO;
using Errors.SR0007_Error.Domain;
using PostSharp.Serialization;

namespace Errors.SR0007_Error;

[PSerializable]
public class Customer : Entity // The serializer of Entity has not been generated.
{
    public string? Name { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var formatter = new PortableFormatter();
        var stream = new MemoryStream();
        formatter.Serialize( new Customer { Id = 1, Name = "Alice" }, stream );
        stream.Position = 0;

        var customer = (Customer) formatter.Deserialize( stream );
        Console.WriteLine( $"Customer {customer.Id}: {customer.Name}" );
    }
}
error SR0007: SR0007_Error.cs(11,14): Cannot apply [PSerializable] to type 'Errors.SR0007_Error.Customer' because the serializer of its base type has not been generated, although the base type is [PSerializable]. Make sure that PostSharp processes the project that defines the base type.

Resolution

Build the project that defines the base type with PostSharp: add a reference to the PostSharp package, and remove the SkipPostSharp property if the project sets it. See PostSharp Components and Well-Known MSBuild Properties.

In the following example, the library is built with PostSharp, and a Customer is serialized and deserialized with its Id and its Name. The code is the same as in the previous example.

using PostSharp.Serialization;

namespace Errors.SR0007_Fixed.Domain;

// This library is built with PostSharp, which generates the serializer of Entity.
[PSerializable]
public class Entity
{
    public int Id { get; set; }
}
using System;
using System.IO;
using Errors.SR0007_Fixed.Domain;
using PostSharp.Serialization;

namespace Errors.SR0007_Fixed;

[PSerializable]
public class Customer : Entity // The serializer of Entity was generated when its library was built.
{
    public string? Name { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var formatter = new PortableFormatter();
        var stream = new MemoryStream();
        formatter.Serialize( new Customer { Id = 1, Name = "Alice" }, stream );
        stream.Position = 0;

        var customer = (Customer) formatter.Deserialize( stream );
        Console.WriteLine( $"Customer {customer.Id}: {customer.Name}" );
    }
}
Customer 1: Alice

See Also

Reference

PSerializableAttribute
PortableFormatter

Other Resources

Understanding Aspect Serialization
PostSharp Components
Well-Known MSBuild Properties