I have an interface that looks like this:
public interface IInterface<T> where T : IParameterType
{
void InterfaceMethod(T param);
}
And a lot of implementations of this interface, each with a different derived type for T.
I can't change the interface.
Now I want all the implementations to call a common line of code at the beginning of its implementation of InterfaceMethod. Furthermore, if possible, I would like all future implementations of this interface to also call this line of code automatically. If this cannot be done, then the best solution would be the one requiring the least knowledge by people writing future implementations.
This seems like it should be easy, but I can't immediately see how to do it.
Edited: Appreciate everyone who answered this. I kept it abstract, but the use case is actually NServiceBus handlers (and a super old version of NServiceBus). To route messages, NServiceBus uses reflection to locate a class that implements IHandleMessages where MessageType:IMessage. IHandleMessages requires a method Handle(MessageType message). For a large class of messages, I wanted to do some initialization before the handler ran.
So as far as I can tell, each individual handler class has to actually have a method Handle(MessageType message), which rules out solutions involving moving the implementation of the interface into a base class.
However I found that the handler actually is instantiated per message received, so I could derive the handlers from a base class and the constructor would be called for each message. There may well have been better ways of doing that by using NServiceBus capabilities, but I am relatively new to it and using a version so old the documentation isn't always applicable.