Why doesn’t this code compile:
public class FakeESBSingleMessage<T> : IBusEnabledClass
{
private Action<T> SubscribedAction { get; set; }
#region IBusEnabledClass
public void Publish<T>(T message)
{
SubscribedAction(message);
}
public void Subscribe<T>(string ID, Action<T> action)
{
SubscribedAction = action;
}
#endregion
}
It give the following errors:
Error 37 Argument 1: cannot convert from ‘T [..FakeESBSingleMessage.cs(8)]’ to ‘T’ ..FakeESBSingleMessage.cs 16 30
Error 39 Cannot implicitly convert type ‘System.Action [mscorlib.dll]’ to ‘System.Action [mscorlib.dll]’ ..FakeESBSingleMessage.cs 21 32
Error 36 Delegate ‘System.Action’ has some invalid arguments ..\FakeESBSingleMessage.cs 16 13
I’m trying to create a fake ESB component for testing, that saves the delegate passed on Subscribe and calls it when publish is called.
You get the compile error because your Publish and Subscribe messages define a new type parameter T, rather than using the one defined in the class. Try this instead: