You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.2 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace NFine.Code
  11. {
  12. /// <summary>
  13. /// 常用公共类
  14. /// </summary>
  15. public class Common
  16. {
  17. #region Stopwatch计时器
  18. /// <summary>
  19. /// 计时器开始
  20. /// </summary>
  21. /// <returns></returns>
  22. public static Stopwatch TimerStart()
  23. {
  24. Stopwatch watch = new Stopwatch();
  25. watch.Reset();
  26. watch.Start();
  27. return watch;
  28. }
  29. /// <summary>
  30. /// 计时器结束
  31. /// </summary>
  32. /// <param name="watch"></param>
  33. /// <returns></returns>
  34. public static string TimerEnd(Stopwatch watch)
  35. {
  36. watch.Stop();
  37. double costtime = watch.ElapsedMilliseconds;
  38. return costtime.ToString();
  39. }
  40. #endregion
  41. #region 删除数组中的重复项
  42. /// <summary>
  43. /// 删除数组中的重复项
  44. /// </summary>
  45. /// <param name="values"></param>
  46. /// <returns></returns>
  47. public static string[] RemoveDup(string[] values)
  48. {
  49. List<string> list = new List<string>();
  50. for (int i = 0; i < values.Length; i++)//遍历数组成员
  51. {
  52. if (!list.Contains(values[i]))
  53. {
  54. list.Add(values[i]);
  55. };
  56. }
  57. return list.ToArray();
  58. }
  59. #endregion
  60. #region 自动生成编号
  61. /// <summary>
  62. /// 表示全局唯一标识符 (GUID)。
  63. /// </summary>
  64. /// <returns></returns>
  65. public static string GuId()
  66. {
  67. return Guid.NewGuid().ToString();
  68. }
  69. /// <summary>
  70. /// 自动生成编号 201008251145409865
  71. /// </summary>
  72. /// <returns></returns>
  73. public static string CreateNo()
  74. {
  75. Random random = new Random();
  76. string strRandom = random.Next(1000, 10000).ToString(); //生成编号
  77. string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
  78. return code;
  79. }
  80. #endregion
  81. #region 生成0-9随机数
  82. /// <summary>
  83. /// 生成0-9随机数
  84. /// </summary>
  85. /// <param name="codeNum">生成长度</param>
  86. /// <returns></returns>
  87. public static string RndNum(int codeNum)
  88. {
  89. StringBuilder sb = new StringBuilder(codeNum);
  90. Random rand = new Random();
  91. for (int i = 1; i < codeNum + 1; i++)
  92. {
  93. int t = rand.Next(9);
  94. sb.AppendFormat("{0}", t);
  95. }
  96. return sb.ToString();
  97. }
  98. #endregion
  99. #region 删除最后一个字符之后的字符
  100. /// <summary>
  101. /// 删除最后结尾的一个逗号
  102. /// </summary>
  103. public static string DelLastComma(string str)
  104. {
  105. return str.Substring(0, str.LastIndexOf(","));
  106. }
  107. /// <summary>
  108. /// 删除最后结尾的指定字符后的字符
  109. /// </summary>
  110. public static string DelLastChar(string str, string strchar)
  111. {
  112. return str.Substring(0, str.LastIndexOf(strchar));
  113. }
  114. /// <summary>
  115. /// 删除最后结尾的长度
  116. /// </summary>
  117. /// <param name="str"></param>
  118. /// <param name="Length"></param>
  119. /// <returns></returns>
  120. public static string DelLastLength(string str, int Length)
  121. {
  122. if (string.IsNullOrEmpty(str))
  123. return "";
  124. str = str.Substring(0, str.Length - Length);
  125. return str;
  126. }
  127. #endregion
  128. }
  129. public static class ConvertHelper
  130. {
  131. #region 文本转换
  132. public static string ToStringExt(this object data)
  133. {
  134. return data == null ? string.Empty : data.ToString();
  135. }
  136. //public static string ToStringExt(this IConvertHelper convert, object data)
  137. //{
  138. // return ToStringExt(data);
  139. //}
  140. #endregion
  141. }
  142. }