2

我有一堂课是这样的:

public static class Messenger<T>
{
    private static readonly Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
    public static void DoSomethingWithEventTable() //Somehow fills eventTable
    public static void Clear() 
    {
        eventTable.Clear();
    }
}

现在,我DoSomethingWithEventTable在程序的某个地方调用了两次,如下所示:

Messenger<int>.DoSomethingWithEventTable();
Messenger<float>.DoSomethingWithEventTable();

我想清除eventTable每个Messenger<T>. 我该怎么做?我是否应该调用Clear我放入泛型中的每种类型,如下所示:

Messenger<int>.Clear();
Messenger<float>.Clear();

或者做一次像这样愚蠢的事情就足够了:

Messenger<string>.Clear();

UPD:基本实验表明,我应该为每个使用过的 T 清除 Messenger。现在有人可以为这些类提供更好的设计吗?

我现在使用的更详细的版本:

static public class Messenger<T>
{
    private static readonly Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

    static public void AddListener(string eventType, Callback<T> handler)
    {
        // Obtain a lock on the event table to keep this thread-safe.
        lock (eventTable)
        {
            // Create an entry for this event type if it doesn't already exist.
            if (!eventTable.ContainsKey(eventType))
            {
                eventTable.Add(eventType, null);
            }
            // Add the handler to the event.
            eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
        }
    }

    static public void RemoveListener(string eventType, Callback<T> handler)
    {
        // Obtain a lock on the event table to keep this thread-safe.
        lock (eventTable)
        {
            // Only take action if this event type exists.
            if (eventTable.ContainsKey(eventType))
            {
                // Remove the event handler from this event.
                eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;

                // If there's nothing left then remove the event type from the event table.
                if (eventTable[eventType] == null)
                {
                    eventTable.Remove(eventType);
                }
            }
        }
    }

    static public void Invoke(string eventType, T arg1)
    {
        Delegate d;
        // Invoke the delegate only if the event type is in the dictionary.
        if (eventTable.TryGetValue(eventType, out d))
        {
            // Take a local copy to prevent a race condition if another thread
            // were to unsubscribe from this event.
            Callback<T> callback = (Callback<T>)d;

            // Invoke the delegate if it's not null.
            if (callback != null)
            {
                callback(arg1);
            }
        }
    }

    static public void Clear()
    {
        eventTable.Clear();
    }
}

同样重要的是我有另一个类Messenger(非泛型,是的)和Messenger<T,M>,也许有一天我什至需要类似的东西Messenger<T,M,N>,等等。

4

2 回答 2

3

每种Messenger<T>类型都有自己的 eventTable 副本,因此您需要为您使用的每个不同的 T 调用 Clear() 。

如本次测试所示:

[TestFixture]
    public class Tests
    {    
        static class MyClass<T>
        {
            public static List<int> Member = new List<int>();
        }

        [Test]
        public void StaticTest()
        {
            var m1 = MyClass<int>.Member;
            var m2 = MyClass<string>.Member;

            Assert.AreNotSame(m1, m2);
        }
}
于 2012-10-26T06:50:20.333 回答
3

自从

private static readonly Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

不依赖于<T>,为所有事件表创建一个静态“处理程序”。

IE

public static class TableHandler {
    ICollection<Dictionary<string, Delegate>> tables = new List<Dictionary<string, Delegate>>();

    public void Add(Dictionary<string, Delegate> item) 
    {
        tables.Add(item);
    }

    public void Clear()
    {
        foreach (var item in tables) item.Clear();
        tables.Clear();
    }
}

并确保DoSomethingWithEventTable()将事件表添加到TableHandler.

可能不是最佳的整体解决方案,但它可以帮助您跟踪当前设计的表格。

编辑:

我试图用谷歌搜索找到静态类的所有通用变体的方法,但我没有找到方法。有谁知道这样做的方法?

于 2012-10-26T06:55:04.733 回答