EdlinSoftware.Generators.ReadOnlyInterface
1.0.0
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
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.0" />
<PackageVersion Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.0" />
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" />
paket add EdlinSoftware.Generators.ReadOnlyInterface --version 1.0.0
#r "nuget: EdlinSoftware.Generators.ReadOnlyInterface, 1.0.0"
#:package EdlinSoftware.Generators.ReadOnlyInterface@1.0.0
#addin nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.0
#tool nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.0
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
WithReadOnlyattribute fromEdlinSoftware.Generators.ReadOnlyInterfacenamespace.
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
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.