/// <summary> /// Creates the db provider. /// </summary> /// <param name="connectionString">Name of the conn STR.</param> /// <returns>The db provider.</returns> public static DbProvider CreateDbProvider(ProviderType providerType, string connectionString) { try { string[] assAndClass = EnumDescriptionAttribute.GetDescription(providerType).Split(','); DbProvider dbProvider; if (assAndClass.Length > 1) { dbProvider = CreateDbProvider(assAndClass[1].Trim(), assAndClass[0].Trim(), connectionString); } else { dbProvider = CreateDbProvider(null, assAndClass[0].Trim(), connectionString); } if (dbProvider != null) { dbProvider.ConnectName = "Default"; } return(dbProvider); } catch { throw; } }
/// <summary> /// 获取枚举类型的描述 /// </summary> /// <param name="enumObj"></param> /// <returns></returns> public static string GetDescription(object enumObj) { Type enumType = enumObj.GetType(); if (!enumType.IsEnum) { throw new Exception("参数不是枚举类型!"); } FieldInfo fieldInfo = enumType.GetField(enumObj.ToString()); object[] attribArray = fieldInfo.GetCustomAttributes(false); if (attribArray.Length == 0) { return(null); } EnumDescriptionAttribute attrib = attribArray[0] as EnumDescriptionAttribute; return(attrib.Description); }
/// <summary> /// 获取枚举类型的所有描述 /// </summary> /// <param name="enumType"></param> /// <returns></returns> public static string[] GetDescriptions(Type enumType) { if (!enumType.IsEnum) { throw new Exception("参数不是枚举类型!"); } FieldInfo[] fieldInfos = enumType.GetFields(); List <string> deslist = new List <string>(); foreach (FieldInfo fieldInfo in fieldInfos) { object[] attribArray = fieldInfo.GetCustomAttributes(false); if (attribArray.Length == 0) { continue; } EnumDescriptionAttribute attrib = attribArray[0] as EnumDescriptionAttribute; deslist.Add(attrib.Description); } return(deslist.ToArray()); }