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'
}
- Problem: An interface with a
static abstract
member cannot be used as a generic, such asList<T>
. - Solution: Replace the
abstract
modifier withvirtual
. - 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.
- Install the package from Nuget
- Give a static interface member the attribute
Svee4.RequiredStaticMembers.RequiredAttribute
- All done! A deriving type that does not implement the given member will cause an error
Please open an issue or discussion
- The repo of Immediate.Handlers was helpful in setting up csproj and CI/CD