Ejemplo n.º 1
0
        /// <summary>
        /// 获取枚举成员的名称
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="member">枚举成员实例或成员值,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A"</param>
        public static string GetMemberName <T>(object member)
        {
            //转成基础类型的成员值
            Type   underlyingType = GetUnderlyingType(typeof(T));
            object memberValue    = ConvertHelper.ConvertTo(member, underlyingType);

            //获取枚举成员的名称
            return(Enum.GetName(typeof(T), memberValue));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取枚举成员的值
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="memberName">枚举成员的常量名,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"获取0</param>
        public static object GetMemberValue <T>(string memberName)
        {
            //获取基础类型
            Type underlyingType = GetUnderlyingType(typeof(T));

            //获取枚举实例
            T instance = GetInstance <T>(memberName);

            //获取枚举成员的值
            return(ConvertHelper.ConvertTo(instance, underlyingType));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 返回Excel第一个工作表表名
        /// </summary>
        /// <param name="connection">excel连接</param>
        /// <returns></returns>
        public static string GetExcelFirstTableName(OleDbConnection connection)
        {
            string tableName = string.Empty;

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            if (dt != null && dt.Rows.Count > 0)
            {
                tableName = ConvertHelper.ConvertTo <string>(dt.Rows[0][2]);
            }

            return(tableName);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 检查设置的端口号是否正确,并返回正确的端口号,无效端口号返回-1。
        /// </summary>
        /// <param name="port">设置的端口号</param>
        public static int GetValidPort(string port)
        {
            //声明返回的正确端口号
            int validPort = -1;

            validPort = ConvertHelper.ConvertTo <int>(port);

            //最小有效端口号
            const int MINPORT = 0;
            //最大有效端口号
            const int MAXPORT = 65535;

            if (validPort <= MINPORT || validPort > MAXPORT)
            {
                throw new ArgumentException("参数port端口号范围无效!");
            }

            return(validPort);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 返回Excel工作表名
        /// </summary>
        /// <param name="connection">excel连接</param>
        /// <returns></returns>
        public static List <string> GetExcelTablesName(OleDbConnection connection)
        {
            List <string> list = new List <string>();

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(ConvertHelper.ConvertTo <string>(dt.Rows[i][2]));
                }
            }

            return(list);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 通过字符串获取枚举成员实例
 /// </summary>
 /// <typeparam name="T">枚举名,比如Enum1</typeparam>
 /// <param name="member">枚举成员的常量名或常量值,
 /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"或"0"获取 Enum1.A 枚举类型</param>
 public static T GetInstance <T>(string member)
 {
     return(ConvertHelper.ConvertTo <T>(Enum.Parse(typeof(T), member, true)));
 }