rspec/rules/S4039/csharp/rule.adoc

83 lines
2.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
When a base type explicitly implements a public interface method, property or event, that member is only accessible in derived types through a reference to the current instance (namely `this`). If the derived type explicitly overrides that interface member, the base implementation becomes inaccessible.
2021-04-28 16:49:39 +02:00
This rule raises an issue when an unsealed, externally visible type provides an explicit member implementation of an `interface` and does not provide an alternate, externally visible member with the same name.
2021-04-28 16:49:39 +02:00
=== Exceptions
This rule does not report a violation for an explicit implementation of `IDisposable.Dispose` when an externally visible `Close()` or `System.IDisposable.Dispose(Boolean)` method is provided.
== How to fix it
2021-04-28 16:49:39 +02:00
Make the class sealed, change the class member to a non-explicit declaration, or provide a new class member exposing the functionality of the explicit interface member.
=== Code examples
2021-04-28 16:49:39 +02:00
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
public interface IMyInterface
{
void MyMethod();
}
public class Foo : IMyInterface
{
void IMyInterface.MyMethod() // Noncompliant
{
MyMethod();
}
}
----
==== Compliant solution
2021-04-28 16:49:39 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
public interface IMyInterface
{
void MyMethod();
}
public class Foo : IMyInterface
{
void IMyInterface.MyMethod()
{
MyMethod();
}
// This method can be public or protected
protected void MyMethod()
2021-04-28 16:49:39 +02:00
{
// Do something ...
}
}
----
== Resources
=== Documentation
2021-04-28 16:49:39 +02:00
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation[Explicit Interface Implementation]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make "XXX" sealed, change to a non explicit declaration or provide a new method exposing the functionality of "YYY".
=== Highlighting
Explicit interface method implementation
endif::env-github,rspecator-view[]