You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtensionsHeadings.cs 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Xml.Linq;
  9. using Novacode;
  10. namespace Novacode
  11. {
  12. public static class ExtensionsHeadings
  13. {
  14. public static Paragraph Heading(this Paragraph paragraph, HeadingType headingType)
  15. {
  16. string StyleName = headingType.EnumDescription();
  17. paragraph.StyleName = StyleName;
  18. return paragraph;
  19. }
  20. public static string EnumDescription(this Enum enumValue)
  21. {
  22. if (enumValue == null || enumValue.ToString() == "0")
  23. {
  24. return string.Empty;
  25. }
  26. FieldInfo enumInfo = enumValue.GetType().GetField(enumValue.ToString());
  27. DescriptionAttribute[] enumAttributes = (DescriptionAttribute[])enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  28. if (enumAttributes.Length > 0)
  29. {
  30. return enumAttributes[0].Description;
  31. }
  32. else
  33. {
  34. return enumValue.ToString();
  35. }
  36. }
  37. /// <summary>
  38. /// From: http://stackoverflow.com/questions/4108828/generic-extension-method-to-see-if-an-enum-contains-a-flag
  39. /// Check to see if a flags enumeration has a specific flag set.
  40. /// </summary>
  41. /// <param name="variable">Flags enumeration to check</param>
  42. /// <param name="value">Flag to check for</param>
  43. /// <returns></returns>
  44. public static bool HasFlag(this Enum variable, Enum value)
  45. {
  46. if (variable == null)
  47. return false;
  48. if (value == null)
  49. throw new ArgumentNullException("value");
  50. // Not as good as the .NET 4 version of this function, but should be good enough
  51. if (!Enum.IsDefined(variable.GetType(), value))
  52. {
  53. throw new ArgumentException(string.Format(
  54. "Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.",
  55. value.GetType(), variable.GetType()));
  56. }
  57. ulong num = Convert.ToUInt64(value);
  58. return ((Convert.ToUInt64(variable) & num) == num);
  59. }
  60. }
  61. }