Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ExtensionsHeadings.cs 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System;
  10. using System.Reflection;
  11. using System.ComponentModel;
  12. namespace Xceed.Words.NET
  13. {
  14. public static class ExtensionsHeadings
  15. {
  16. public static Paragraph Heading( this Paragraph paragraph, HeadingType headingType )
  17. {
  18. var description = headingType.EnumDescription();
  19. paragraph.StyleName = description;
  20. return paragraph;
  21. }
  22. public static string EnumDescription( this Enum enumValue )
  23. {
  24. if( (enumValue == null) || (enumValue.ToString() == "0") )
  25. return string.Empty;
  26. var enumInfo = enumValue.GetType().GetField( enumValue.ToString() );
  27. var enumAttributes = ( DescriptionAttribute[] )enumInfo.GetCustomAttributes( typeof( DescriptionAttribute ), false );
  28. if( enumAttributes.Length > 0 )
  29. return enumAttributes[ 0 ].Description;
  30. else
  31. return enumValue.ToString();
  32. }
  33. public static bool HasFlag( this Enum variable, Enum value )
  34. {
  35. if( variable == null )
  36. return false;
  37. if( value == null )
  38. throw new ArgumentNullException( "value" );
  39. if( !Enum.IsDefined( variable.GetType(), value ) )
  40. throw new ArgumentException( string.Format( "Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.", value.GetType(), variable.GetType() ) );
  41. var num = Convert.ToUInt64( value );
  42. return ( ( Convert.ToUInt64( variable ) & num ) == num );
  43. }
  44. }
  45. }