123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Bowin.Common.Utility;
- namespace Bowin.Common.Linq
- {
- public static class stringExtensions
- {
- /// <summary>
- /// 把字符串转成对应该类型(自动去左右空格)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="text"></param>
- /// <returns></returns>
- public static Nullable<T> ConvertLinqData<T>(this string text) where T : struct
- {
- text = text.Trim();
- var convertvalue = new Nullable<T>();
- if (text.Length > 0 || typeof(T) == typeof(string))
- {
- convertvalue = new Nullable<T>((T)Convert.ChangeType(text, typeof(T)));
- }
- return convertvalue;
- }
- public static object ParseToType(this string text, Type targetType)
- {
- text = text.Trim();
- var valueTargetType = targetType;
- if (valueTargetType.Name.StartsWith("Nullable")) valueTargetType = targetType.GetGenericArguments()[0];
- var converterType = typeof(WebExtensions);
- var converter = converterType.GetMethod("ParseTo", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
- converter = converter.MakeGenericMethod(valueTargetType);
- return converter.Invoke(null, new object[] { text });
- }
- }
- }
|