Esempio n. 1
0
        public static TimeSpan ToTimeSpan(object p)
        {
            TimeSpan ts = default(TimeSpan);

            TimeSpan.TryParse(CommOp.ToStr(p), out ts);
            return(ts);
        }
Esempio n. 2
0
        /// <summary>
        /// 按全角2个单位半角1个单位截取指定长度单位的字符串
        /// </summary>
        /// <param name="s">原字符串</param>
        /// <param name="len">保留的长度</param>
        /// <returns>截取后的字符串</returns>
        public static string CutByteStr(String s, int len)
        {
            if (s == null)
            {
                return("");
            }
            if (CommOp.ByteLength(s) <= len)
            {
                return(s);
            }

            StringBuilder sb = new StringBuilder();
            int           l  = 0;

            foreach (char c in s)
            {
                l++;
                if (c > 127)
                {
                    l++;
                }
                if (l >= len)
                {
                    break;
                }
                sb.Append(c);
            }
            return(sb.ToString());
        }
Esempio n. 3
0
 /// <summary>
 /// 将字符串分割成长整数数组
 /// </summary>
 /// <param name="ints"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 public static long[] ToLongArray(string ints, char c)
 {
     string[] sArr = ints.Split(c);
     long[]   iArr = new long[sArr.Length];
     for (int i = 0; i < sArr.Length; i++)
     {
         iArr[i] = CommOp.ToLong(sArr[i]);
     }
     return(iArr);
 }
Esempio n. 4
0
 /// <summary>
 /// 更新对象中字段的值
 /// </summary>
 /// <param name="obj">对象</param>
 /// <param name="fi">字段信息</param>
 /// <param name="value">值</param>
 /// <param name="actualType">值的类型,为空时自动判断</param>
 static void SetFieldValue(object obj, FieldInfo fi, string value, Type actualType = null)
 {
     if (fi == null)
     {
         return;
     }
     if (String.IsNullOrEmpty(value))
     {
         return;
     }
     if (PraserDict.ContainsKey(fi.FieldType))
     {
         var pvalue = PraserDict[fi.FieldType].Prase(value);
         if (pvalue == null)
         {
             return;
         }
         fi.SetValue(obj, pvalue);
         return;
     }
     if (fi.FieldType == typeof(string) || fi.FieldType == typeof(object)) //字段类型为String
     {
         fi.SetValue(obj, value);
     }
     else if (fi.FieldType == typeof(int) || fi.FieldType == typeof(Int64) || fi.FieldType == typeof(Int16))
     {
         fi.SetValue(obj, CommOp.ToLong(value));
     }
     else if (fi.FieldType == typeof(bool))
     {
         fi.SetValue(obj, CommOp.ToBool(value));
     }
     else if (fi.FieldType == typeof(double) || fi.FieldType == typeof(float))
     {
         fi.SetValue(obj, CommOp.ToDouble(value));
     }
     else if (fi.FieldType == typeof(decimal))
     {
         fi.SetValue(obj, CommOp.ToDecimal(value));
     }
     else if (fi.FieldType.IsEnum)
     {
         if (fi.FieldType.IsEnumDefined(value ?? ""))
         {
             fi.SetValue(obj, Enum.Parse(fi.FieldType, value));
         }
     }
     else
     {
         actualType = actualType ?? fi.FieldType;
         var p = JsonHelper.FormJson(value, actualType) ?? Activator.CreateInstance(actualType);
         fi.SetValue(obj, p);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 设置一个属性
 /// </summary>
 /// <param name="propName">属性名称</param>
 /// <param name="o">属性值</param>
 void SetValue(String propName, object o)
 {
     if (o == null)
     {
         propTable.Remove(propName);
     }
     else
     {
         propTable[propName] = CommOp.ToStr(o);
     }
 }
Esempio n. 6
0
 /// <summary>
 /// 将字符串分割成整数数组
 /// </summary>
 /// <param name="ints"></param>
 /// <param name="c"></param>
 /// <returns></returns>
 public static int[] ToIntArray(string ints, char c)
 {
     if (ints.IsEmpty())
     {
         return(new int[0]);
     }
     string[] sArr = ints.Split(c);
     int[]    iArr = new int[sArr.Length];
     for (int i = 0; i < iArr.Length; i++)
     {
         iArr[i] = CommOp.ToInt(sArr[i]);
     }
     return(iArr);
 }
Esempio n. 7
0
        /// <summary>
        /// 转换成字符,如果是一个字符串,则取第一个字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static char ToChar(object s)
        {
            if (s == null || s == DBNull.Value || CommOp.ToStr(s) == "")
            {
                return(default(char));
            }
            if (s is char)
            {
                return((char)s);
            }

            string cs = s.ToString();

            return(cs.Length == 0 ? default(char) : cs[0]);
        }
Esempio n. 8
0
        /// <summary>
        /// 生成邮件信息对象
        /// </summary>
        /// <returns>邮件信息对象</returns>
        protected System.Net.Mail.MailMessage MakeMessage()
        {
            //MailMessage message = new MailMessage(From, To, Subject, Body);
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.From = new MailAddress(From);
            string[] emails = To.Replace(',', ';').Split(';');
            foreach (string email in emails)
            {
                if (!string.IsNullOrEmpty(email) && CommOp.IsEmail(email))
                {
                    message.To.Add(email);
                }
            }
            CC     = CC ?? "";
            emails = CC.Replace(',', ';').Split(';');
            foreach (string email in emails)
            {
                if (!string.IsNullOrEmpty(email) && CommOp.IsEmail(email))
                {
                    message.CC.Add(email);
                }
            }

            message.Subject = Subject;
            //message.SubjectEncoding = Encoding.UTF8;
            message.Body = Body;
            //message.BodyEncoding = Encoding.UTF8;

            //添加文件附件
            if (!Files.IsEmpty())
            {
                foreach (string file in Files)
                {
                    Attachment att = new Attachment(file);

                    //att.NameEncoding = Encoding.UTF8;
                    //att.Name = new FileInfo(file).Name;

                    message.Attachments.Add(att);
                }
            }
            message.IsBodyHtml = true;
            return(message);
        }
Esempio n. 9
0
 /// <summary>
 /// 将reader[Key]转换为字符串
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string ToStr(this IDataReader reader, string key)
 {
     return(CommOp.ToStr(reader[key]));
 }
Esempio n. 10
0
 /// <summary>
 /// 将DataRow中的值转换成double
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static double ToDouble(this DataRow dr, string key)
 {
     return(CommOp.ToDouble(dr[key]));
 }
Esempio n. 11
0
 /// <summary>
 /// 将reader[Key]转换为Int32
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static int ToInt(this IDataReader reader, string key)
 {
     return(CommOp.ToInt(reader[key]));
 }
Esempio n. 12
0
 /// <summary>
 /// 将reader[index]转换为字符串
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static string ToStr(this IDataReader reader, int index)
 {
     return(CommOp.ToStr(reader[index]));
 }
Esempio n. 13
0
 /// <summary>
 /// 将DataRow中的值转换为特定类型
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static T ToValue <T>(this DataRow dr, string key)
 {
     return(CommOp.HackType <T>(dr[key]));
 }
Esempio n. 14
0
 /// <summary>
 /// 将DataRow中的值转换为特定类型
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dr"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static T ToValue <T>(this DataRow dr, int index)
 {
     return(CommOp.HackType <T>(dr[index]));
 }
Esempio n. 15
0
 /// <summary>
 /// 将DataRow中的值转换成bool?类型
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static bool?ToBoolNull(this DataRow dr, string key)
 {
     return(CommOp.ToBoolNull(dr[key]));
 }
Esempio n. 16
0
 /// <summary>
 /// 将DataRow中的值转换成bool?类型
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static bool?ToBoolNull(this DataRow dr, int index)
 {
     return(CommOp.ToBoolNull(dr[index]));
 }
Esempio n. 17
0
 /// <summary>
 /// 将DataRow中的值转换成去除两边空格后的String,不会返回null
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string ToStr(this DataRow dr, string key)
 {
     return(CommOp.ToStr(dr[key]));
 }
Esempio n. 18
0
 public static string ToStr(this DataRow dr, int index)
 {
     return(CommOp.ToStr(dr[index]));
 }
Esempio n. 19
0
 /// <summary>
 /// 将reader[index]转换为Int32
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static int ToInt(this IDataReader reader, int index)
 {
     return(CommOp.ToInt(reader[index]));
 }
Esempio n. 20
0
 /// <summary>
 /// 将DataRow中的值转换成Int32
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static int ToInt(this DataRow dr, string key)
 {
     return(CommOp.ToInt(dr[key]));
 }
Esempio n. 21
0
 /// <summary>
 /// 将DataRow中的值转换成double
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static double ToDouble(this DataRow dr, int index)
 {
     return(CommOp.ToDouble(dr[index]));
 }
Esempio n. 22
0
 /// 将DataRow中的值转换成Int32
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static long?ToLongNull(this DataRow dr, string key)
 {
     return(CommOp.ToLongNull(dr[key]));
 }
Esempio n. 23
0
 /// <summary>
 /// 将DataRow中的值转换成int?
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static long?ToLongNull(this DataRow dr, int index)
 {
     return(CommOp.ToLongNull(dr[index]));
 }
Esempio n. 24
0
 /// <summary>
 /// 将DataRow中的值转换成Int32
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public static int ToInt(this DataRow dr, int index)
 {
     return(CommOp.ToInt(dr[index]));
 }
Esempio n. 25
0
        /// <summary>
        /// 将value中的值赋给对象的属性或字段
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="propName">属性名</param>
        /// <param name="value">值</param>
        /// <param name="actualType">值的类型,为空时自动判断</param>
        public static void SetValue(object obj, string propName, string value, Type actualType = null)
        {
            //if (String.IsNullOrEmpty(value)) return;
            FieldInfo fi = obj.GetType().GetField(propName, bindAttr);

            if (fi != null)
            {
                SetFieldValue(obj, fi, value, actualType);
                return;
            }
            PropertyInfo pi = GetPropertyInfo(obj, propName);

            if (pi == null)
            {
                return;
            }
            if (!pi.CanWrite)
            {
                return;
            }
            if (PraserDict.ContainsKey(pi.PropertyType))
            {
                var pvalue = PraserDict[pi.PropertyType].Prase(value);
                if (pvalue == null)
                {
                    return;
                }
                pi.SetValue(obj, pvalue, null);
                return;
            }
            if (pi.PropertyType == typeof(string) || pi.PropertyType == typeof(object))
            {
                pi.SetValue(obj, value, null);
            }
            else if (pi.PropertyType == typeof(int))
            {
                pi.SetValue(obj, CommOp.ToInt(value), null);
            }
            else if (pi.PropertyType == typeof(Int64))
            {
                pi.SetValue(obj, CommOp.ToLong(value), null);
            }
            else if (pi.PropertyType == typeof(Int16))
            {
                pi.SetValue(obj, (short)CommOp.ToInt(value), null);
            }
            else if (pi.PropertyType == typeof(int?))
            {
                pi.SetValue(obj, CommOp.ToIntNull(value), null);
            }
            else if (pi.PropertyType == typeof(bool))
            {
                pi.SetValue(obj, CommOp.ToBool(value), null);
            }
            else if (pi.PropertyType == typeof(bool?))
            {
                pi.SetValue(obj, CommOp.ToBoolNull(value), null);
            }
            else if (pi.PropertyType == typeof(char))
            {
                pi.SetValue(obj, CommOp.ToChar(value), null);
            }
            else if (pi.PropertyType == typeof(double) || pi.PropertyType == typeof(float))
            {
                pi.SetValue(obj, CommOp.ToDouble(value), null);
            }
            else if (pi.PropertyType == typeof(decimal))
            {
                pi.SetValue(obj, CommOp.ToDecimal(value), null);
            }
            else if (pi.PropertyType.IsEnum)
            {
                if (CommOp.IsNumeric(value))
                {
                    var val = CommOp.ToInt(value);
                    if (pi.PropertyType.IsEnumDefined(val))
                    {
                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, val.ToString()), null);
                    }
                }
                else
                {
                    var val = CommOp.ToStr(value);
                    if (pi.PropertyType.IsEnumDefined(val))
                    {
                        pi.SetValue(obj, Enum.Parse(pi.PropertyType, val), null);
                    }
                }
            }
            else if (pi.PropertyType == typeof(DateTime))
            {
                DateTime dt = CommOp.ToDateTime(value);
                if (dt == default(DateTime))
                {
                    return;
                }
                pi.SetValue(obj, dt, null);
            }
            else if (pi.PropertyType == typeof(DateTime?))
            {
                DateTime?dt = CommOp.ToDateTimeNull(value);
                pi.SetValue(obj, dt, null);
            }
            else
            {
                actualType = actualType ?? pi.PropertyType;
                var p = JsonHelper.FormJson(value, actualType) ?? Activator.CreateInstance(actualType);
                try
                {
                    pi.SetValue(obj, p, null);
                }
                catch
                {
                    throw;
                }
            }
        }