ACategory.cs 902 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Common.Helper;
  6. namespace Model
  7. {
  8. public abstract class ACategory<T>: ICategory where T : AConfig
  9. {
  10. private Dictionary<int, T> dict;
  11. public virtual void BeginInit()
  12. {
  13. this.dict = new Dictionary<int, T>();
  14. string path = Path.Combine(@"../../Config/", typeof (T).Name);
  15. if (!Directory.Exists(path))
  16. {
  17. throw new Exception($"not found config path: {path}");
  18. }
  19. foreach (string file in Directory.GetFiles(path))
  20. {
  21. T t = MongoHelper.FromJson<T>(File.ReadAllText(file));
  22. this.dict.Add(t.Id, t);
  23. }
  24. }
  25. public Type ConfigType
  26. {
  27. get
  28. {
  29. return typeof (T);
  30. }
  31. }
  32. public virtual void EndInit()
  33. {
  34. }
  35. public T this[int type]
  36. {
  37. get
  38. {
  39. return this.dict[type];
  40. }
  41. }
  42. public T[] GetAll()
  43. {
  44. return this.dict.Values.ToArray();
  45. }
  46. }
  47. }