using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bowin.Common.Utility;
namespace Bowin.Common.Linq
{
public static class stringExtensions
{
///
/// 把字符串转成对应该类型(自动去左右空格)
///
///
///
///
public static Nullable ConvertLinqData(this string text) where T : struct
{
text = text.Trim();
var convertvalue = new Nullable();
if (text.Length > 0 || typeof(T) == typeof(string))
{
convertvalue = new Nullable((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 });
}
}
}