Depends on what this method is doing. Why you don't use a third class which has this method? Then you can have an instance of it in the class and the record and call it.
public interface IExample
{
void Foo();
}
public class ExampleClass: IExample
{
private ExampleService service;
public void Foo() => service.Foo();
}
public record ExampleRecord : IExample
{
private ExampleService service;
public void Foo() => service.Foo();
}
public class ExampleService
{
public void Foo()
{
// ...
}
}
Another way is a default interface implementation which is available since C#8:
public interface IExample
{
void Foo()
{
// ...
}
}
public class ExampleClass: IExample
{
}
public record ExampleRecord : IExample
{
}
ExampleClass ec = new();
ExampleRecord er = new();
((IExample)ec).Foo();
((IExample)er).Foo();
As you can see you need an explicit cast to the interface, otherwise it will not compile. Why