FlagsEnumTool.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. namespace Y.Utils.Net20.EnumUtils
  3. {
  4. /// <summary>
  5. /// 标志枚举修改工具
  6. /// 弃用:效率太低
  7. /// sa = sa | StatusAttributes.Join;//添加属性
  8. /// sa = (sa | StatusAttributes.Share) ^ StatusAttributes.Share;//删除属性
  9. /// </summary>
  10. [Obsolete]
  11. public sealed class FlagsEnumTool
  12. {
  13. public static int AddAttribute(int en, int att)
  14. {
  15. return en ^ att;
  16. }
  17. public static int AddAttribute<T>(T en, T att)
  18. {
  19. try
  20. {
  21. int intEn = (int)Convert.ChangeType(en, typeof(int));
  22. int intAtt = (int)Convert.ChangeType(att, typeof(int));
  23. return intEn ^ intAtt;
  24. }
  25. catch (Exception e) { }
  26. return 0;
  27. }
  28. public static int RemoveAttribute(int en, int att)
  29. {
  30. return (en | att) ^ att;
  31. }
  32. public static int RemoveAttribute<T>(T en, T att)
  33. {
  34. try
  35. {
  36. int intEn = (int)Convert.ChangeType(en, typeof(int));
  37. int intAtt = (int)Convert.ChangeType(att, typeof(int));
  38. return (intEn | intAtt) ^ intAtt;
  39. }
  40. catch (Exception e) { }
  41. return 0;
  42. }
  43. }
  44. }