/// <summary> /// Convert cell value to desired type, including nullable structs. /// When converting blank string to nullable struct (e.g. ' ' to int?) null is returned. /// When attempted conversion fails exception is passed through. /// </summary> /// <typeparam name="T"> /// The type to convert to. /// </typeparam> /// <returns> /// The <paramref name="value"/> converted to <typeparamref name="T"/>. /// </returns> /// <remarks> /// If input is string, parsing is performed for output types of DateTime and TimeSpan, which if fails throws <see cref="FormatException"/>. /// Another special case for output types of DateTime and TimeSpan is when input is double, in which case <see cref="DateTime.FromOADate"/> /// is used for conversion. This special case does not work through other types convertible to double (e.g. integer or string with number). /// In all other cases 'direct' conversion <see cref="Convert.ChangeType(object, Type)"/> is performed. /// </remarks> /// <exception cref="FormatException"> /// <paramref name="value"/> is string and its format is invalid for conversion (parsing fails) /// </exception> /// <exception cref="InvalidCastException"> /// <paramref name="value"/> is not string and direct conversion fails /// </exception> public static T GetTypedCellValue <T>(object value) { var conversion = new TypeConvertUtil <T>(value); if (value == null || (conversion.ReturnType.IsNullable && conversion.Value.IsEmptyString)) { return(default);
/// <summary> /// save object from formdata, using default convert util /// </summary> protected T save <T, t>(string formdata) where T : Obj <t> { return(QueryObject <T, t> .Save(formdata, delegate(T obj, NameValueCollection nv) { TypeConvertUtil.ConvertFrom(obj, nv); return true; })); }
private static object execute(object obj, MethodInfo mi, NameValueCollection nv) { object ret; ParameterInfo[] paras = mi.GetParameters(); if (paras.Length == 1 && paras[0].ParameterType == typeof(NameValueCollection)) { ret = mi.Invoke(obj, new object[] { nv }); } else if (paras.Length == 0) { ret = mi.Invoke(obj, null); } else { List <object> p = new List <object>(); foreach (var item in paras) { if (item.ParameterType.IsSubclassOf(typeof(Array))) { string v = nv[item.Name] ?? nv[item.Name + "[]"]; string[] strs = StringUtil.CommaDelimitedListToStringArray(v); Array array = Array.CreateInstance(item.ParameterType.GetElementType(), strs.Length); for (int i = 0; i < strs.Length; i++) { array.SetValue(TypeConvertUtil.ConvertTo(strs[i], item.ParameterType.GetElementType()), i); } p.Add(array); } else if (item.ParameterType == typeof(MobileDetect)) { p.Add(MobileDetect.Instance); } else { string v = nv[item.Name]; p.Add(TypeConvertUtil.ConvertTo(v, item.ParameterType)); } } ret = mi.Invoke(obj, p.ToArray()); } return(ret); }
private void AssembleParam <T>(Dictionary <Type, Dictionary <string, string> > paramDict, object[] paramObjects, int i) where T : ParameterAttribute { ParameterAttribute paramAttr = parametersDict[i].GetCustomAttribute <T>(false); if (paramAttr != null) { string paramName = paramAttr.Name; if (!string.IsNullOrWhiteSpace(paramName)) { paramObjects[i] = TypeConvertUtil.ConvertToBasicType(parametersDict[i].ParameterType, paramDict[paramAttr.GetType()][paramName]); } else { paramObjects[i] = TypeConvertUtil.ConvertToBasicType(parametersDict[i].ParameterType, paramDict[paramAttr.GetType()][parametersDict[i].Name]); } } }
/// <summary> /// 将指定的值转换为指定的类型。 /// </summary> /// <param name="value">要转换的值。</param> /// <param name="conversionType">要转换的类型。</param> /// <returns></returns> public static object CastTo(this object value, Type conversionType) { return(TypeConvertUtil.CastTo(value, conversionType)); }
/// <summary> /// 将指定的值转换为指定的类型。 /// </summary> /// <typeparam name="T">泛型类型。</typeparam> /// <param name="value">要转换的值。</param> /// <returns></returns> public static T CastTo <T>(this object value) { return(TypeConvertUtil.CastTo <T>(value)); }
public static List <T> ImportFromXml(XmlDocument xml) { Type t = typeof(T); List <PropertyInfo> props = new List <PropertyInfo>(t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)); List <T> list = new List <T>(); XmlNode node = xml.DocumentElement.SelectSingleNode("//" + t.FullName); if (node == null) { return(list); } foreach (XmlNode n in node.ChildNodes) { T obj = Activator.CreateInstance <T>(); foreach (XmlAttribute attr in n.Attributes) { PropertyInfo pi = props.Find(p => { return(p.Name.Equals(attr.Name, StringComparison.InvariantCultureIgnoreCase)); }); if (pi == null || !pi.CanWrite) { continue; } pi.SetValue(obj, TypeConvertUtil.ConvertTo(attr.Value, pi.PropertyType), null); } list.Add(obj); } ILinqContext <T> context = CreateContext(); // remove old items // List <t> ids = new List <t>(); foreach (var item in list) { ids.Add(item.Id); } List <T> old_list = Gets(context, StringUtil.CollectionToCommaDelimitedString(ids)); foreach (var item in old_list) { if (list.Find((T obj) => { return(item.Id.Equals(obj.Id)); }) != null) { context.Remove(item); } } // add new items // foreach (var item in list) { context.Add(item, true); } context.SubmitChanges(true); return(list); }