Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svee4/RequiredStaticMembers
Roslyn analyzer to enforce types to implement static virtual interface members
https://github.com/svee4/RequiredStaticMembers
Last synced: 25 days ago
JSON representation
Roslyn analyzer to enforce types to implement static virtual interface members
- Host: GitHub
- URL: https://github.com/svee4/RequiredStaticMembers
- Owner: svee4
- License: mit
- Created: 2024-02-04T00:35:11.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-03-15T17:22:48.000Z (10 months ago)
- Last Synced: 2024-03-15T18:40:59.108Z (10 months ago)
- Language: C#
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
- RSCG_Examples - https://github.com/svee4/RequiredStaticMembers
README
# RequiredStaticMembers
[![NuGet](https://img.shields.io/nuget/v/Svee4.RequiredStaticMembers.svg?style=plastic)](https://www.nuget.org/packages/Svee4.RequiredStaticMembers/)
[![GitHub license](https://img.shields.io/github/license/svee4/RequiredStaticMembers.svg)](https://github.com/svee4/RequiredStaticMembers/blob/main/license.txt)## Enforce types to implement static virtual interface members
```cs
using Svee4.RequiredStaticMembers;interface INode
{
[Required]
public static virtual string Color => RequiredStaticMemberAccessException.Throw(nameof(Color));
}class GreenNode : INode
{
// No error - property is implemented as expected
public static string Color => "Green";
}class BlueNode : INode
{
// Error RSM001: Type 'BlueNode' does not implement required static member 'GetColor' from interface 'INode'
}
```## Why?
- Problem: An interface with a `static abstract` member cannot be used as a generic, such as `List`.
- Solution: Replace the `abstract` modifier with `virtual`.
- Problem: Deriving classes are no longer required to implement the member. The interface must provide an implementation, likely one that throws an exception at
runtime.
- Solution: Use `[RequiredAttribute]` to enforce all deriving classes to implement the static member, making accidental calls to the default implementation
impossible.## How do i use it?
1. Install the package from [Nuget](https://www.nuget.org/packages/Svee4.RequiredStaticMembers/)
2. Give a static interface member the attribute `Svee4.RequiredStaticMembers.RequiredAttribute`
3. All done! A deriving type that does not implement the given member will cause an error## I have an issue or I want to participate in development
Please open an issue or discussion
## Acknowledgments
- The repo of [Immediate.Handlers](https://github.com/viceroypenguin/Immediate.Handlers) was helpful in setting up csproj and CI/CD