AccountInfoCollection.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Concurrent;
  4. namespace ZcPeng.weixin.PublicAccount
  5. {
  6. /// <summary>
  7. /// 公众号集合
  8. /// </summary>
  9. public static class AccountInfoCollection
  10. {
  11. /// <summary>
  12. /// 已知的公众号信息
  13. /// </summary>
  14. private static ConcurrentDictionary<string, AccountInfo> accountInfos = new ConcurrentDictionary<string, AccountInfo>();
  15. /// <summary>
  16. /// 获取公众号信息
  17. /// </summary>
  18. /// <param name="userName">公众号</param>
  19. /// <returns>返回公众号信息;如果公众号不存在,返回null。</returns>
  20. public static AccountInfo GetAccountInfo(string userName)
  21. {
  22. return accountInfos.ContainsKey(userName) ? accountInfos[userName] : null;
  23. }
  24. /// <summary>
  25. /// 设置公众号信息
  26. /// </summary>
  27. /// <param name="account">公众号信息</param>
  28. public static void SetAccountInfo(AccountInfo account)
  29. {
  30. if (account == null)
  31. throw new ArgumentNullException("account", "公众号信息不能为空。");
  32. accountInfos[account.UserName] = account;
  33. }
  34. /// <summary>
  35. /// 获取已知的公众号
  36. /// </summary>
  37. public static ICollection<string> UserNames
  38. {
  39. get
  40. {
  41. return accountInfos.Keys;
  42. }
  43. }
  44. /// <summary>
  45. /// 获取已知的公众号信息
  46. /// </summary>
  47. public static ICollection<AccountInfo> AccountInfos
  48. {
  49. get
  50. {
  51. return accountInfos.Values;
  52. }
  53. }
  54. /// <summary>
  55. /// 获取第一个已知的公众号信息
  56. /// </summary>
  57. public static AccountInfo First
  58. {
  59. get
  60. {
  61. AccountInfo account = null;
  62. if (accountInfos.Count > 0)
  63. {
  64. foreach (KeyValuePair<string, AccountInfo> pair in accountInfos)
  65. {
  66. account = pair.Value;
  67. break;
  68. }
  69. }
  70. return account;
  71. }
  72. }
  73. }
  74. }