Object - IsEnum
A T extension method that query if '@this' is enum.
public enum Color { Red, Blue, Green } public static void Main() { string str = ""; string[] strArray = {"a", "b", "c"}; List<int> list = new List<int>(); IEnumerable<Char> charList = new List<char> (){ 'a', 'b' }; object[] objList = { str, strArray, list, charList, Color.Red }; foreach (var t in objList) { //C# Extension Method: Object - IsEnum Console.WriteLine("{0,-15} IsEnum = {1}", t.GetType(), t.IsEnum()); } }
View Source
public static partial class Extensions { /// <summary> /// A T extension method that query if '@this' is enum. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>true if enum, false if not.</returns> public static bool IsEnum<T>(this T @this) { return @this.GetType().IsEnum; } }