EdlinSoftware.Generators.ReadOnlyInterface 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EdlinSoftware.Generators.ReadOnlyInterface --version 1.0.0
                    
NuGet\Install-Package EdlinSoftware.Generators.ReadOnlyInterface -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EdlinSoftware.Generators.ReadOnlyInterface --version 1.0.0
                    
#r "nuget: EdlinSoftware.Generators.ReadOnlyInterface, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EdlinSoftware.Generators.ReadOnlyInterface@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.0
                    
Install as a Cake Tool

Read-only interface generator

This repository contains Roslyn generator of read-only interfaces.

Russian version of documentation

Installation

Add EdlinSoftware.Generators.ReadOnlyInterface NuGet package to your project:

dotnet add package EdlinSoftware.Generators.ReadOnlyInterface

Generation of read-only interface

If you want to generate a read-only interface for your type, you must do the following things:

  • Make your type partial,
  • Augment it with WithReadOnly attribute from EdlinSoftware.Generators.ReadOnlyInterface namespace.
using EdlinSoftware.Generators.ReadOnlyInterface;

[WithReadOnly]
internal partial class SimpleClass
{
    public long Id { get; set; }

    public string Name { get; set; } = string.Empty;

    public DateTime BirthDate { get; set; }

    public List<string> Hobbies { get; set; } = new();
}

Properties of generated read-only interface

The generated interface will contain all public readable properties of your type, including inherited properties from base types.

Supported types

You can generate read-only interface for:

  • Classes
  • Structures
  • Records
  • Interfaces
[WithReadOnly]
internal partial struct SimpleStruct
{
    public long Id { get; set; }

    public string Name { get; set; }
}

Nested types

You can generate read-only interface for a nested type. But in this case you must make partial not only the type itself, but all containing types:

internal partial class Container
{
    [WithReadOnly]
    private partial class Nested
    {
        public long Id { get; set; }
    }
}

Type accessibility

Read-only interface can be generated for a type of any accessibility:

  • public
  • protected
  • internal
  • protected internal
  • private

By default, generated read-only interface has the same accessibility as its underlying type.

But you also may change accessibility of generated read-only interface. It can be done using Accessibility property of WithReadOnly attribute:

partial class AccessibilityContainer
{
    [WithReadOnly(Accessibility = ReadOnlyInterfaceAccessibility.Public)]
    private partial record PublicAccessibility(long Id);
}

As new read-only interface will be generated inside the same containing type, as your underlying type, it is your responsibility to provide valid interface accessibility for this place.

Name of read-only interface

By default the name of a read-only interface is generated from the name of underlying type by adding ReadOnly prefix. For example, for a class with name Person there will be generated a read-only interface with name IReadOnlyPerson. For an interface with name IEntity there will be generated a read-only interface with name IReadOnlyEntity.

But you have an ability to change this prefix. It can be done using Prefix property of WithReadOnly attribute:

[WithReadOnly(Prefix = "IFrozen")]
partial class PrefixedClass
{
    public string Name { get; set; } = string.Empty;
}

In this case, you are fully responsible for providing a prefix which forms a valid .NET name of type.

Appreciations

NuGet package icon is created by Freepik on Flaticon

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 194 2/20/2025
1.0.0 182 12/28/2024