This compiles:
public interface IMyInterface
{
event Action<dynamic> OnSomeEvent;
}
class MyInterface : IMyInterface
{
public event Action<dynamic> OnSomeEvent;
}
But when i separate the interface and the implementation to different projects, i get:
Accessor
‘TestProject2.MyInterface.OnSomeEvent.remove’
cannot implement interface member
‘InterfaceNamespace.IMyInterface.remove_OnSomeEvent(System.Action)’
for type ‘TestProject2.MyInterface’.
Use an explicit interface
implementation.
This occurs only with a dynamic parameter…
Good catch. This looks like it’s possibly a bug in the C# compiler – I’ll ping Eric Lippert to see what he thinks. (
dynamiccan be a bit tricksy; there may well be a perfectly good but non-obvious reason for this error.)EDIT: The code below appears not to work after all. I could have sworn I had it working this morning… I’m very confused as to what’s going on. As per Simon’s comments, the code fails with a message saying it’s not supported by the language.
Note that if you do use explicit interface implementation, it appears to compile just fine:
EDIT: The rest of this answer still stands…
Note that you can’t specify a field-like event as an explicitly-implemented event, i.e. this doesn’t work:
It gives the following error message:
And if you just try to change to the event accessor syntax, you get the same error as the
original code.
Note that changing the event to a property works fine with an auto-implemented property implementation.