/// <summary> /// String 转 Enum /// by zpc v1.0 /// </summary> /// <typeparam name="T">对应的Enum</typeparam> /// <param name="value">要转的string</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static T ToEnum<T>(this string value, T defaultValue) where T : struct { if (string.IsNullOrEmpty(value)) { return defaultValue; } T result; return Enum.TryParse<T>(value, true, out result) ? result : defaultValue; }
原创转载请标注作者