Is Valid Enum Value - CSharp System

CSharp examples for System:Enum

Description

Is Valid Enum Value

Demo Code


using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System;/*from   w ww. j a va  2 s  .  co  m*/

public class Main{
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="U"></typeparam>
      /// <param name="value"></param>
      /// <param name="ignoreCase"></param>
      /// <returns></returns>
      public static bool IsValidEnumValue<U> (string value, bool ignoreCase) where U : struct
      {
         if (value == null || value.Length == 0)
            return false;
         try {
            U u = (U)Enum.Parse (typeof (U), value, ignoreCase);
            return IsValidEnumValue<U> (u);
         } catch (InvalidCastException) {
            return false;
         }
      }
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="U"></typeparam>
      /// <param name="value"></param>
      /// <returns></returns>
      public static bool IsValidEnumValue<U> (string value) where U : struct
      {
         if (value == null || value.Length == 0)
            return false;

         return IsValidEnumValue<U> (value, false);
      }
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="U"></typeparam>
      /// <param name="value"></param>
      /// <returns></returns>
      public static bool IsValidEnumValue<U> (object value) where U : struct
      {
         if (value == null)
            return false;

         return Enum.IsDefined (typeof (U), value);
      }
}

Related Tutorials