I’m trying to open an 5.4.1f1 project into Uinty 5.5.1f1 and a piece of code of mine is now triggering an error. This is the code :
public interface IMessageDispatcher{
void Dispatch<T>(T param);
void AddListener<T>(Action<T> callback);
void RemoveListener<T> (Action<T> callback);
}
public class MessageDispatcher<T> : IMessageDispatcher{
public string type;
public Action<T> listeners;
public void Dispatch<T>(T param){
if (listeners != null) {
Action<T> current = listeners as Action<T>;
current (param);
}
}
public void AddListener<T>(Action<T> callback){
listeners += callback;
}
public void RemoveListener<T>(Action<T> callback){
listeners -= callback;
}
}
And this is the error message :
So I tried to change the listeners parameter to public event Action listeners in order to have “+” and “-” operators working as they are in the 5.4.1f1 project. After that the editor shows a new error which I do not know how to resolve.
It’s seems I’m trying to convert something to another thing which is the same thing … Not really clear for me and this is where I need a little help
I had a quick look and found doing this gets rid of all errors and warnings although I have no idea if it works correctly at runtime.
public interface IMessageDispatcher<T>{
void Dispatch(T param);
void AddListener(Action<T> callback);
void RemoveListener(Action<T> callback);
}
public class MessageDispatcher<T> : IMessageDispatcher<T>{
public string type;
public Action<T> listeners;
public void Dispatch(T param){
if (listeners != null) {
Action<T> current = listeners as Action<T>;
current (param);
}
}
public void AddListener(Action<T> callback){
listeners += callback;
}
public void RemoveListener(Action<T> callback){
listeners -= callback;
}
}
I believe the issue in the first post was that in the interface you declared a generic type and labeled “T” and then in the implementing class you declared another generic type and also labeled it “T”. This second Action was actually (to help explain what the compiler was seeing) Action and thus the Addlistener code read like this to the compiler:
Action<U> listeners += Action<T> callback;
there is no operator to add a Action to an Action. it was only confusing because the compiler printed them out as both Action.
This is why your new code fixed the issue, the interface and implementor are now both using the same generic “T”.
thanks for your help, I will try again to open up my project in 5.5.1f1 and see if it works. I didn’t thought at first that between 5.4 and 5.5 a same code could be valid or not.
I didn’t realize at that time but @andymads solution can’t work in my case. I need to be able to declare a Dictionary containing IMessageDispatcher like this :
IDictionary<Type,IMessageDispatcher> subscribers = new Dictionary<Type,IMessageDispatcher>
If the IMessageDispatcher class becomes “IMessageDispatcher” it would be impossible to declare a dictionary like this because the type T won’t be known at compile time.
add an abstract interface type and use that in your dictionary. and then the functions which would be using the subscribers need to be generic functions. like so
public interface IAbstractDispatcher{}
public interface IMessageDispatcher<T>:IAbstractDispatcher
{
//interface code here
}
public class MessageDispatcher<T> : IMessageDispatcher<T>
{
//implement dispatcher code here
}
public class EventHandler
{
private IDictionary<Type,IAbstractDispatcher> subscribers = new Dictionary<Type,IAbstractDispatcher>;
public void Add<T>(Action<T> call)
{
Type type = getType(T);
IAbstractDispatcher dispatcher;
if(!subscribers.TryGetValue(type,out dispatcher))
{
dispatcher = new MessageDispatcher<T>();
subscribers.Add(type,dispatcher);
}
var castedDispatcher = (IMessageDispatcher<T>) dispatcher;
castedDispatcher.AddListener(call);
}
}