Get the description of a Enum value. : IP Address « Network « C# / C Sharp






Get the description of a Enum value.

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;

namespace LinkedIn.Utility
{
  /// <summary>
  /// A helper class for enums.
  /// </summary>
  public static class EnumHelper
  {
    /// <typeparam name="TValue">usually int</typeparam>
    public static List<TValue> GetValues<TEnum, TValue>()
    {
      List<TValue> values = new List<TValue>();
      Array array = Enum.GetValues(typeof(TEnum));
      foreach (TValue item in array)
      {
        values.Add(item);
      }

      return values;
    }

    /// <summary>
    /// Get the description of a <see cref="Enum" /> value.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>A description of the <see cref="Enum" /> value.</returns>
    public static string GetDescription(Enum value)
    {
      FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
      DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fieldInfo.GetCustomAttributes(
            typeof(DescriptionAttribute), false);

      return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
    }

    /// <summary>
    /// </summary>
    /// <typeparam name="TEnum"></typeparam>
    /// <param name="enumeratedType"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool HasFlag<TEnum>(this TEnum enumeratedType, TEnum value)
        where TEnum : struct, IComparable, IFormattable, IConvertible
    {
      if ((enumeratedType is Enum) == false)
      {
        throw new InvalidOperationException("Struct is not an Enum.");
      }

      if (typeof(TEnum).GetCustomAttributes(
          typeof(FlagsAttribute), false).Length == 0)
      {
        throw new InvalidOperationException("Enum must use [Flags].");
      }

      long enumValue = enumeratedType.ToInt64(CultureInfo.InvariantCulture);
      long flagValue = value.ToInt64(CultureInfo.InvariantCulture);

      if ((enumValue & flagValue) == flagValue)
      {
        return true;
      }

      return false;
    }
  }
}

   
    
  








Related examples in the same category

1.IPAddress AddressFamily
2.Parse an IPAddress
3.Loopback IPAddress
4.Broadcast IPAddress
5.Any IPAddress
6.None IPAddress
7.IsLoopback IPAddress
8.IP Address parse, lookup IP Address parse, lookup
9.IPEndPoint sampleIPEndPoint sample
10.Get Host By Name, Get Host Name
11.Get Host Entry
12.Parse Host String
13.Get Current Ip Address
14.Get Host IP Address
15.Get IP address by query whatismyip.com
16.Get Local IP Address
17.IP to value
18.Get user IP from HttpContext
19.IP to Uint
20.UInt32 To IP Address
21.IP Address To UInt 32
22.Get IP address from notation (xxx.xxx.xxx.xxx) or hostname
23.Gets my local IP address (not necessarily external) and subnet mask
24.Returns true if the IPEndPoint supplied is on the same subnet as this host
25.Is Ip Address By Regular Expression
26.Checks if a given string is a valid IP address.
27.IP Address To NumberIP Address To Number
28.Get Subnet Mask
29.Get Available Socket Port