ConfigManager.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. namespace Model
  6. {
  7. public class ConfigManager
  8. {
  9. public Dictionary<string, object> allConfig;
  10. public ConfigManager(Assembly assembly)
  11. {
  12. Load(assembly);
  13. }
  14. public void Load(Assembly assembly)
  15. {
  16. var localAllConfig = new Dictionary<string, object>();
  17. Type[] types = assembly.GetTypes();
  18. foreach (var type in types)
  19. {
  20. object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
  21. if (attrs.Length == 0)
  22. {
  23. continue;
  24. }
  25. object obj = (Activator.CreateInstance(type));
  26. var iSupportInitialize = obj as ISupportInitialize;
  27. if (iSupportInitialize != null)
  28. {
  29. iSupportInitialize.EndInit();
  30. }
  31. localAllConfig[type.Name] = obj;
  32. }
  33. this.allConfig = localAllConfig;
  34. }
  35. public T Get<T>(int type) where T : IType
  36. {
  37. var configCategory = (ConfigCategory<T>)this.allConfig[typeof(T).Name];
  38. return configCategory[type];
  39. }
  40. public Dictionary<int, T> GetAll<T>() where T : IType
  41. {
  42. var configCategory = (ConfigCategory<T>)this.allConfig[typeof(T).Name];
  43. return configCategory.GetAll();
  44. }
  45. public ConfigCategory<T> GetConfigCategory<T>() where T : IType
  46. {
  47. return (ConfigCategory<T>) this.allConfig[typeof (T).Name];
  48. }
  49. }
  50. }