CSharp examples for System:Enum
Convert Object To Enum
using System.Reflection; using System.Data; using System.Text; using System.Linq; using System.Collections.Generic; using System;/* www . ja v a 2 s . co m*/ public class Main{ public static T ToEnum<T>(object obj, T defaultEnum) { string str = To<string>(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; } }