字符串、数值 等类型的相互 安全转换

转换方法代码:

  1 #region SafeParse
  2         public static bool SafeParseBool(object target, bool defaultValue)
  3         {
  4             if (target == null) return defaultValue;
  5             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
  6             return SafeParseBool(tmp, defaultValue);
  7         }
  8         public static bool SafeParseBool(string text, bool defaultValue)
  9         {
 10             bool flag;
 11             if (bool.TryParse(text, out flag))
 12             {
 13                 defaultValue = flag;
 14             }
 15             return defaultValue;
 16         }
 17 
 18         public static DateTime SafeParseDateTime(object target, DateTime defaultValue)
 19         {
 20             if (target == null) return defaultValue;
 21             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
 22             return SafeParseDateTime(tmp, defaultValue);
 23         }
 24         public static DateTime SafeParseDateTime(string text, DateTime defaultValue)
 25         {
 26             DateTime time;
 27             if (DateTime.TryParse(text, out time))
 28             {
 29                 defaultValue = time;
 30             }
 31             return defaultValue;
 32         }
 33 
 34         public static decimal SafeParseDecimal(object target, decimal defaultValue)
 35         {
 36             if (target == null) return defaultValue;
 37             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
 38             return SafeParseDecimal(tmp, defaultValue);
 39         }
 40         public static decimal SafeParseDecimal(string text, decimal defaultValue)
 41         {
 42             decimal num;
 43             if (decimal.TryParse(text, out num))
 44             {
 45                 defaultValue = num;
 46             }
 47             return defaultValue;
 48         }
 49 
 50         public static short SafeParseShort(object target, short defaultValue)
 51         {
 52             if (target == null) return defaultValue;
 53             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
 54             return SafeParseShort(tmp, defaultValue);
 55         }
 56         public static short SafeParseShort(string text, short defaultValue)
 57         {
 58             short num;
 59             if (short.TryParse(text, out num))
 60             {
 61                 defaultValue = num;
 62             }
 63             return defaultValue;
 64         }
 65 
 66         public static int SafeParseInt(object target, int defaultValue)
 67         {
 68             if (target == null) return defaultValue;
 69             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
 70             return SafeParseInt(tmp, defaultValue);
 71         }
 72         public static int SafeParseInt(string text, int defaultValue)
 73         {
 74             int num;
 75             if (int.TryParse(text, out num))
 76             {
 77                 defaultValue = num;
 78             }
 79             return defaultValue;
 80         }
 81         public static int[] SafeParseInt(string[] text, int defaultValue)
 82         {
 83             if (text == null || text.Length < 1) return new[] { defaultValue };
 84 
 85             int[] nums = new int[text.Length];
 86             int tmp;
 87             for (int i = 0; i < text.Length; i++)
 88             {
 89                 if (int.TryParse(text[i], out tmp)) nums[i] = tmp;
 90                 else nums[i] = defaultValue;
 91             }
 92             return nums;
 93         }
 94         public static string SafeParseIntFilter(string text, int defaultValue, char split = ',')
 95         {
 96             if (string.IsNullOrWhiteSpace(text)) return defaultValue.ToString(CultureInfo.InvariantCulture);
 97             string[] tmpSplit = text.Split(new[] { split }, StringSplitOptions.RemoveEmptyEntries);
 98             if (tmpSplit.Length < 1) return defaultValue.ToString(CultureInfo.InvariantCulture);
 99 
100             int tmp;
101             for (int i = 0; i < tmpSplit.Length; i++)
102             {
103                 if (int.TryParse(tmpSplit[i], out tmp))
104                     tmpSplit[i] = tmp.ToString(CultureInfo.InvariantCulture);
105                 else
106                     tmpSplit[i] = defaultValue.ToString(CultureInfo.InvariantCulture);
107             }
108             return string.Join(split.ToString(CultureInfo.InvariantCulture), tmpSplit);
109         }
110 
111         public static long SafeParseLong(object target, long defaultValue)
112         {
113             if (target == null) return defaultValue;
114             string tmp = target.ToString(); if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
115             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
116             return SafeParseLong(tmp, defaultValue);
117         }
118         public static long SafeParseLong(string text, long defaultValue)
119         {
120             long num;
121             if (long.TryParse(text, out num))
122             {
123                 defaultValue = num;
124             }
125             return defaultValue;
126         }
127 
128         public static long[] SafeParseLong(string[] text, long defaultValue)
129         {
130             if (text == null || text.Length < 1) return new[] { defaultValue };
131 
132             long[] nums = new long[text.Length];
133             long tmp;
134             for (int i = 0; i < text.Length; i++)
135             {
136                 if (long.TryParse(text[i], out tmp)) nums[i] = tmp;
137                 else nums[i] = defaultValue;
138             }
139             return nums;
140         }
141 
142         public static string SafeParseLongFilter(string text, long defaultValue, char split = ',')
143         {
144             if (string.IsNullOrWhiteSpace(text)) return defaultValue.ToString(CultureInfo.InvariantCulture);
145             string[] tmpSplit = text.Split(new[] { split }, StringSplitOptions.RemoveEmptyEntries);
146             if (tmpSplit.Length < 1) return defaultValue.ToString(CultureInfo.InvariantCulture);
147 
148             long tmp;
149             for (int i = 0; i < tmpSplit.Length; i++)
150             {
151                 if (long.TryParse(tmpSplit[i], out tmp))
152                     tmpSplit[i] = tmp.ToString(CultureInfo.InvariantCulture);
153                 else
154                     tmpSplit[i] = defaultValue.ToString(CultureInfo.InvariantCulture);
155             }
156             return string.Join(split.ToString(CultureInfo.InvariantCulture), tmpSplit);
157         }
158 
159         public static string SafeParseString(object target, string defaultValue)
160         {
161             if (null != target && "" != target.ToString())
162             {
163                 return target.ToString();
164             }
165             return defaultValue;
166         }
167 
168         #region SafeParseNullParse
169         public static bool? SafeParseBool(object target, bool? defaultValue)
170         {
171             if (target == null) return defaultValue;
172             string tmp = target.ToString();
173             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
174             return SafeParseBool(tmp, defaultValue);
175         }
176         public static bool? SafeParseBool(string text, bool? defaultValue)
177         {
178             bool flag;
179             if (bool.TryParse(text, out flag))
180             {
181                 defaultValue = flag;
182             }
183             return defaultValue;
184         }
185 
186         public static DateTime? SafeParseDateTime(object target, DateTime? defaultValue)
187         {
188             if (target == null) return defaultValue;
189             string tmp = target.ToString();
190             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
191             return SafeParseDateTime(tmp, defaultValue);
192         }
193         public static DateTime? SafeParseDateTime(string text, DateTime? defaultValue)
194         {
195             DateTime time;
196             if (DateTime.TryParse(text, out time))
197             {
198                 defaultValue = time;
199             }
200             return defaultValue;
201         }
202 
203         public static decimal? SafeParseDecimal(object target, decimal? defaultValue)
204         {
205             if (target == null) return defaultValue;
206             string tmp = target.ToString();
207             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
208             return SafeParseDecimal(tmp, defaultValue);
209         }
210         public static decimal? SafeParseDecimal(string text, decimal? defaultValue)
211         {
212             decimal num;
213             if (decimal.TryParse(text, out num))
214             {
215                 defaultValue = num;
216             }
217             return defaultValue;
218         }
219 
220         public static short? SafeParseShort(object target, short? defaultValue)
221         {
222             if (target == null) return defaultValue;
223             string tmp = target.ToString();
224             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
225             return SafeParseShort(tmp, defaultValue);
226         }
227         public static short? SafeParseShort(string text, short? defaultValue)
228         {
229             short num;
230             if (short.TryParse(text, out num))
231             {
232                 defaultValue = num;
233             }
234             return defaultValue;
235         }
236 
237         public static int? SafeParseInt(object target, int? defaultValue)
238         {
239             if (target == null) return defaultValue;
240             string tmp = target.ToString();
241             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
242             return SafeParseInt(tmp, defaultValue);
243         }
244         public static int? SafeParseInt(string text, int? defaultValue)
245         {
246             int num;
247             if (int.TryParse(text, out num))
248             {
249                 defaultValue = num;
250             }
251             return defaultValue;
252         }
253 
254         public static long? SafeParseLong(object target, long? defaultValue)
255         {
256             if (target == null) return defaultValue;
257             string tmp = target.ToString();
258             if (string.IsNullOrWhiteSpace(tmp)) return defaultValue;
259             return SafeParseLong(tmp, defaultValue);
260         }
261         public static long? SafeParseLong(string text, long? defaultValue)
262         {
263             long num;
264             if (long.TryParse(text, out num))
265             {
266                 defaultValue = num;
267             }
268             return defaultValue;
269         }
270         #endregion
271 
272         #region SafeParseEnum
273         /// <summary>
274         /// 将枚举数值or枚举名称 安全转换为枚举对象
275         /// </summary>
276         /// <typeparam name="T">枚举类型</typeparam>
277         /// <param name="value">数值or名称</param>
278         /// <param name="defaultValue">默认值</param>
279         /// <remarks>转换区分大小写</remarks>
280         /// <returns></returns>
281         public static T SafeParseEnum<T>(string value, T defaultValue) where T : struct
282         {
283             return SafeParseEnum<T>(value, defaultValue, false);
284         }
285 
286         /// <summary>
287         /// 将枚举数值or枚举名称 安全转换为枚举对象
288         /// </summary>
289         /// <typeparam name="T">枚举类型</typeparam>
290         /// <param name="value">数值or名称</param>
291         /// <param name="defaultValue">默认值</param>
292         /// <param name="ignoreCase">是否忽略大小写 true 不区分大小写 | false 区分大小写</param>
293         /// <returns></returns>
294         public static T SafeParseEnum<T>(string value, T defaultValue, bool ignoreCase) where T : struct
295         {
296             T result;
297             if (Enum.TryParse<T>(value, ignoreCase, out result))
298             {
299                 if (Enum.IsDefined(typeof(T), result))
300                 {
301                     defaultValue = result;
302                 }
303             }
304             return defaultValue;
305         }
306         #endregion
307 
308         #endregion

 

posted @ 2016-02-29 17:50  赛跑的蜗牛  阅读(338)  评论(0编辑  收藏  举报