FormDictionaryTool.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.7.31 - 2017.7.31
  5. // desc: Form唯一字典工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. namespace Y.Utils.DataUtils.Collections
  15. {
  16. /// <summary>
  17. /// Form唯一字典工具
  18. /// </summary>
  19. public class FormDictionaryTool
  20. {
  21. protected ConcurrentDictionary<Type, Form> dictionary = new ConcurrentDictionary<Type, Form>();
  22. public T Get<T>() where T : Form
  23. {
  24. if (dictionary.ContainsKey(typeof(T)))
  25. {
  26. Form value;
  27. if (dictionary.TryGetValue(typeof(T), out value))
  28. {
  29. if (value != null && !value.IsDisposed)
  30. {
  31. return (T)value;
  32. }
  33. }
  34. }
  35. T form = default(T);
  36. if (dictionary.TryAdd(typeof(T), form))
  37. {
  38. return form;
  39. }
  40. return null;
  41. }
  42. }
  43. }