CSharp examples for System:Enum
To Enum
using System.Reflection; using System.Data; using System.Text; using System.Linq; using System.Collections.Generic; using System;// w w w . j a va 2s .c o m public class Main{ public static T ToEnum<T>(object obj, T defaultEnum) { string str = ToString(obj); if (Enum.IsDefined(typeof(T),str)) return (T)Enum.Parse(typeof(T),str); int num; if (int.TryParse(str, out num)) { if (Enum.IsDefined(typeof(T), num)) return (T)Enum.ToObject(typeof(T), num); } return defaultEnum; } }