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.2KB

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