read-only-interface-generator

0

Описание

This repository contains Roslyn generator of read-only interfaces.

Языки

  • C#93,2%
  • PowerShell3,6%
  • Shell3%
  • Batchfile0,2%
README.md

Build status

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:

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.

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

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:

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:

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:

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

Types of properties of read-only interface

Usually types of properties of the read only interface are the same as in the initial class. But there can be a case, when such type has read-only interface itself:

Here

Entity
property of
AnotherClass
has type
EntityClass
which itself has read-only interface. In this case, generated interface will change type of this property to corresponding read-only interface:

If you don't want such behavior, you can use

NotReplaceWithReadOnly
attribute:

You can set this attribute to particular properties or to the whole type:

Appreciations

NuGet package icon is created by Freepik on Flaticon