using System;
using System.Collections.Generic;
using System.Text;
namespace BITKit
{
/// 泛型事件,简单的事件接口
public interface IGenericEvent
{
void AddListener(Action action);
void Invoke(T value);
void Invoke() where T : new();
void RemoveListener(Action action);
void AddListener(TKey key, Action action);
void Invoke(TKey key, T value);
void RemoveListener(TKey key, Action action);
void SetDirect(int key, object value);
void GetDirect(int key, out object value);
void GetDirect(int key,out T value);
}
/// 泛型数据库,主要使用方式为Get和Set
public interface IDatabase
{
T Get(string key = Constant.System.Internal);
void Set(T value);
void Set(string key = Constant.System.Internal, T value = default);
}
public class GenericEvent : IGenericEvent, IDatabase
{
private readonly Dictionary _directDirection = new();
private readonly Dictionary> events = new();
private readonly Dictionary dictionary = new();
public const string defaultEventName = nameof(GenericEvent);
public void Clear()
{
_directDirection.Clear();
events.Clear();
dictionary.Clear();
}
public void AddListener(Action action) =>
AddListener(defaultEventName, action);
public void Invoke(T value) =>
Invoke(defaultEventName, value);
public void RemoveListener(Action action) =>
RemoveListener(defaultEventName, action);
public void AddListener(string key, Action action)
{
key = key.GetType();
var list = events.Get(key);
list.Add(action);
}
public void AddListenerDirect(string key, Action