2012-10-26 27 views
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>

+1

我没有看到你在任何地方使用T ...它用于什么? – LightStriker

+1

该类实际上更大,并且具有类似于'public static void AddListener(string eventType,Callback handler)'的方法',但这确实超出了问题的范围。 –

+0

@Rafal如果它们不是静态的,那么评论会更有意义。但由于它们是静态的,它们实际上是从静态成员的角度分离出“类”。 – flindeberg

回答

3

每个Messenger<T>类型都有它的事件表的自己的副本,所以你需要调用清除()为您使用的每个不同的T。

如通过该测试:

[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); 
     } 
} 
+0

@Rafal,不,你其实是错的。尝试自己或阅读此例如:http://weblogs.asp.net/whaggard/archive/2004/09/05/225955.aspx。我明白你的意图不是混淆。 – flindeberg

+0

@Rafal,我意识到它是静态的,但创建了静态成员的多个副本。看看我已经添加的例子。 –

+0

@flindeberg,打我吧! –

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

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

编辑:

我想谷歌的方式来找到一个静态类的所有通用的变种,但我没有找到一个方法。有谁知道一种方法来做到这一点?