This is an add-in for Fody which contains a workaround for Interface Default Implementation Base Invocation
.
The feature has not been implemented by C# team yet.
The status for it can be seen in dotnet/csharplang#2337
-
Include the
Fody
andInterfaceBaseInvoke.Fody
NuGet packages with aPrivateAssets="all"
attribute on their<PackageReference />
items. InstallingFody
explicitly is needed to enable weaving.<PackageReference Include="Fody" Version="..." PrivateAssets="all" /> <PackageReference Include="InterfaceBaseInvoke.Fody" Version="..." PrivateAssets="all" />
-
If you already have a
FodyWeavers.xml
file in the root directory of your project, add the<InterfaceBaseInvoke />
tag there. This file will be created on the first build if it doesn't exist:<?xml version="1.0" encoding="utf-8" ?> <Weavers> <InterfaceBaseInvoke /> </Weavers>
See Fody usage for general guidelines, and Fody Configuration for additional options.
Call the extension method Base<T>
to cast an object to one of its interfaces, and then call a method or a property.
Just like:
var result = this.Base<Interface>().Method(1, "test");
var value = this.Base<Interface>().Property
-
Unit tests can also serve as examples of API usage. See test cases for valid usage and invalid usage.
-
Basic example:
public interface IService { int Property => 5; void Method() => Console.WriteLine("Calling..."); } public class Service : IService { // call the implementation of interface's property public int Property => this.Base<IService>().Property + 1; public void Method() { Console.WriteLine("Before call method"); // call the implementation of interface's method this.Base<IService>().Method(); Console.WriteLine("After call method"); } }