Reverser.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Reflection;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System;
  5. /// <summary>
  6. /// 继承IComparer<T>接口,实现同一自定义类型 对象比较
  7. /// </summary>
  8. /// <typeparam name="T">T为泛用类型</typeparam>
  9. public class Reverser<T> : IComparer<T>
  10. {
  11. private Type type = null;
  12. private ReverserInfo info;
  13. /// <summary>
  14. /// 构造函数
  15. /// </summary>
  16. /// <param name="type">进行比较的类类型</param>
  17. /// <param name="name">进行比较对象的属性名称</param>
  18. /// <param name="direction">比较方向(升序/降序)</param>
  19. public Reverser(Type type, string name, ReverserInfo.Direction direction)
  20. {
  21. this.type = type;
  22. this.info.name = name;
  23. if (direction != ReverserInfo.Direction.ASC)
  24. this.info.direction = direction;
  25. }
  26. /// <summary>
  27. /// 构造函数
  28. /// </summary>
  29. /// <param name="className">进行比较的类名称</param>
  30. /// <param name="name">进行比较对象的属性名称</param>
  31. /// <param name="direction">比较方向(升序/降序)</param>
  32. public Reverser(string className, string name, ReverserInfo.Direction direction)
  33. {
  34. try
  35. {
  36. this.type = Type.GetType(className, true);
  37. this.info.name = name;
  38. this.info.direction = direction;
  39. }
  40. catch (Exception e)
  41. {
  42. throw new Exception(e.Message);
  43. }
  44. }
  45. /// <summary>
  46. /// 构造函数
  47. /// </summary>
  48. /// <param name="t">进行比较的类型的实例</param>
  49. /// <param name="name">进行比较对象的属性名称</param>
  50. /// <param name="direction">比较方向(升序/降序)</param>
  51. public Reverser(T t, string name, ReverserInfo.Direction direction)
  52. {
  53. this.type = t.GetType();
  54. this.info.name = name;
  55. this.info.direction = direction;
  56. }
  57. //必须!实现IComparer<T>的比较方法。
  58. int IComparer<T>.Compare(T t1, T t2)
  59. {
  60. object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
  61. object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
  62. if (this.info.direction != ReverserInfo.Direction.ASC)
  63. Swap(ref x, ref y);
  64. return (new CaseInsensitiveComparer()).Compare(x, y);
  65. }
  66. //交换操作数
  67. private void Swap(ref object x, ref object y)
  68. {
  69. object temp = null;
  70. temp = x;
  71. x = y;
  72. y = temp;
  73. }
  74. }
  75. /// <summary>
  76. /// 对象比较时使用的信息类
  77. /// </summary>
  78. public struct ReverserInfo
  79. {
  80. /// <summary>
  81. /// 比较的方向,如下:
  82. /// ASC:升序
  83. /// DESC:降序
  84. /// </summary>
  85. public enum Direction
  86. {
  87. ASC = 0,
  88. DESC,
  89. };
  90. public enum Target
  91. {
  92. CUSTOMER = 0,
  93. FORM,
  94. FIELD,
  95. SERVER,
  96. };
  97. public string name;
  98. public Direction direction;
  99. public Target target;
  100. }