如何在词典中添加"event Action<X,Y,Z>"?
How to Add a "event Action<X,Y,Z>" to Dictionary?
我有这两个接口:
public interface IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
}
public interface IEventListener
{
void Listen(EventInfo receiveInfo, EntityEvent.Type eventType, Property property);
}
我也有这两个Class:
public class EventManager : IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property){};
}
public class EventListener : IEventListener
{
public virtual void Listen(EventInfo receiveInfo, EntityEvent.Type eventType, Property property)
{
}
}
还有这些 class:
public class EventInstaller : MonoBehaviour
{
private Dictionary<IEvent,Action<EventInfo, EntityEvent.Type, EntityEvent.Property>> eventSender;
EntityEvent.Property>();
private Dictionary<IEventListener, Action<EventInfo, EntityEvent.Type, EntityEvent.Property>> eventListerner;
public void AddSender(IEvent sender)
{
eventSender.Add(sender, sender.OnEvent);//-->error here
}
public void AddListener(IEventListener listener)
{
eventListerner.Add(listener, listener.Listen);
}
我想这样做:
public void Connect()
{
foreach (IEvent sender in eventSender.Keys)
{
foreach (IEventListener listener in eventListerner.Keys)
{
eventSender[sender] += eventListerner[listener];
}
}
}
错误说:
事件'OnEvent'只能出现在+=或-=
的左边
如何解决这个错误?
非常感谢!!
//---------------------------------------- ------
更新
//---------------------------------------- ------
我这样做是为了解决问题:
public interface IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> GetEvent();
}
public class EventManager : IEvent
{
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> GetEvent()
{
return OnEvent;
}
}
public void AddSender(IEvent sender)
{
eventSender.Add(sender,sender.GetEvent() );
}
if i do this i have the original reference or is a different instance?..
Magic 正在从 IEvent
中删除 event
关键字。它阻止此委派从 class
外部调用。此外,Interface
只能包含方法和属性。将字段转换为 属性.
public interface IEvent
{
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent { get; set; };
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
}
我有这两个接口:
public interface IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
}
public interface IEventListener
{
void Listen(EventInfo receiveInfo, EntityEvent.Type eventType, Property property);
}
我也有这两个Class:
public class EventManager : IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property){};
}
public class EventListener : IEventListener
{
public virtual void Listen(EventInfo receiveInfo, EntityEvent.Type eventType, Property property)
{
}
}
还有这些 class:
public class EventInstaller : MonoBehaviour
{
private Dictionary<IEvent,Action<EventInfo, EntityEvent.Type, EntityEvent.Property>> eventSender;
EntityEvent.Property>();
private Dictionary<IEventListener, Action<EventInfo, EntityEvent.Type, EntityEvent.Property>> eventListerner;
public void AddSender(IEvent sender)
{
eventSender.Add(sender, sender.OnEvent);//-->error here
}
public void AddListener(IEventListener listener)
{
eventListerner.Add(listener, listener.Listen);
}
我想这样做:
public void Connect()
{
foreach (IEvent sender in eventSender.Keys)
{
foreach (IEventListener listener in eventListerner.Keys)
{
eventSender[sender] += eventListerner[listener];
}
}
}
错误说:
事件'OnEvent'只能出现在+=或-=
的左边如何解决这个错误? 非常感谢!!
//---------------------------------------- ------
更新
//---------------------------------------- ------
我这样做是为了解决问题:
public interface IEvent
{
public event Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent;
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> GetEvent();
}
public class EventManager : IEvent
{
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> GetEvent()
{
return OnEvent;
}
}
public void AddSender(IEvent sender)
{
eventSender.Add(sender,sender.GetEvent() );
}
if i do this i have the original reference or is a different instance?..
Magic 正在从 IEvent
中删除 event
关键字。它阻止此委派从 class
外部调用。此外,Interface
只能包含方法和属性。将字段转换为 属性.
public interface IEvent
{
public Action<EventInfo, EntityEvent.Type, EntityEvent.Property> OnEvent { get; set; };
public void SendEvent(EventInfo sendInfo, EntityEvent.Type eventType, Property property);
}