public bool WriteNullable(string value, object target) { var nc = new NullableConverter(Property.PropertyType); // FormatException (thrown by ConvertFromString) is thrown as Exception.InnerException, so we've to catch directly System.Exception try { Property.SetValue(target, nc.ConvertFromString(null, _parsingCulture, value), null); } catch (Exception) { return(false); } return(true); }
/// <summary> /// The safely convert. /// </summary> /// <param name="obj"> /// The obj. /// </param> /// <returns> /// </returns> public static object SafelyConvert(object obj, Type type, object fallbackValue) { if (obj == null) { return(fallbackValue); } try { if (type.IsEnum) { return(Enum.Parse(type, obj.ToString())); } if (obj is string && type == typeof(DateTime)) { var str = (string)obj; return(Convert.ChangeType(DateTime.Parse(str), type)); } if (type == typeof(string)) { return(Convert.ChangeType(obj.ToString(), type)); } if (IsNullableType(type)) { var converter = new NullableConverter(type); return(converter.ConvertFromString(obj.ToString())); } var ret = Convert.ChangeType(obj, type); if (ret == null && type == typeof(string)) { return(Convert.ChangeType(string.Empty, type)); } return(ret); } catch { return(fallbackValue); } }
private bool SetNullableValue(string value, object options) { var nc = new NullableConverter(_field.FieldType); try { lock (_setValueLock) { _field.SetValue(options, nc.ConvertFromString(null, CultureInfo.InvariantCulture, value)); } } // the FormatException (thrown by ConvertFromString) is thrown as Exception.InnerException, // so we've catch directly Exception catch (Exception) { return(false); } return(true); }
/// <summary> /// 根据属性信息获取对应的值类型 /// </summary> /// <param name="pi"></param> /// <param name="inValue"></param> /// <returns></returns> public static object GetPropertyValue(PropertyInfo pi, object inValue) { object value = inValue; Type Temp_t = pi.PropertyType; if (Temp_t.IsGenericType && Temp_t.GetGenericTypeDefinition() == typeof(Nullable <>)) { NullableConverter nullableConverter = new NullableConverter(Temp_t); value = nullableConverter.ConvertFromString(value.ToString()); } else if (pi.PropertyType == typeof(byte)) { value = Convert.ToByte(value); } else if (pi.PropertyType == typeof(int)) { string Temp_strValue = Convert.ToString(value); value = string.IsNullOrEmpty(Temp_strValue) ? 0 : Convert.ToInt32(value); } else if (pi.PropertyType == typeof(DateTime)) { value = Convert.ToDateTime(value); } else if (pi.PropertyType.IsEnum) { value = Convert.ToInt32(value); } else if (pi.PropertyType == typeof(UInt16)) { value = Convert.ToUInt16(value); } else if (inValue == DBNull.Value) { value = null; } else if (pi.PropertyType == typeof(bool)) { value = ConvertClass.GetBooleanValue(value); } return(value); }
public static object ChangeType(object objValue, Type conversionType) { object value = null; if (!conversionType.GetTypeInfo().IsGenericType) { value = conversionType.Name.Equals("Guid") ? Guid.Parse(objValue.ToString()) : (conversionType.GetTypeInfo().IsEnum ? Enum.Parse(conversionType, objValue.ToString()) : Convert.ChangeType(objValue, conversionType)); //; } else { var genericTypeDefinition = conversionType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { var converter = new NullableConverter(conversionType); value = converter.ConvertFromString(objValue.ToString()); } } return(value); }
public static Nullable <Int64> NullableInt64(object obj) { NullableConverter converter = new NullableConverter(typeof(Int64?)); return((Int64?)converter.ConvertFromString(obj.ToString())); }
public static Nullable <int> NullableInt(object obj) { NullableConverter converter = new NullableConverter(typeof(int?)); return((int?)converter.ConvertFromString(obj.ToString())); }
/// <summary> /// 将动态类型转为指定类型实体 /// </summary> /// <param name="data"></param> /// <param name="type"></param> /// <returns></returns> private static object DynamicToEntity(ExpandoObject data, Type type) { var entity = Activator.CreateInstance(type); var dic = data as IDictionary <string, object>; foreach (var item in dic) { if (item.Value == null) { continue; } var type_value = item.Value.GetType(); if (type_value == typeof(DBNull)) { continue; } var prop = type.GetProperty(item.Key, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); if (prop == null) { continue; } object value = item.Value; if (type_value != prop.PropertyType) { if (type_value == typeof(List <object>)) { var valueList = Activator.CreateInstance(prop.PropertyType); var dicValueList = value as List <object>; foreach (var valueItem in dicValueList) { prop.PropertyType.GetMethod("Add") .Invoke(valueList, new object[] { DynamicToEntity(valueItem as ExpandoObject, prop.PropertyType.GenericTypeArguments[0]) }); } value = valueList; } else if (type_value == typeof(ExpandoObject)) { value = DynamicToEntity(item.Value as ExpandoObject, prop.PropertyType); } else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { NullableConverter newNullableConverter = new NullableConverter(prop.PropertyType); try { if (!newNullableConverter.CanConvertFrom(item.Value.GetType())) { value = Convert.ChangeType(item.Value, newNullableConverter.UnderlyingType); } else { value = newNullableConverter.ConvertFrom(item.Value); } } #pragma warning disable CA1031 // Do not catch general exception types catch { value = newNullableConverter.ConvertFromString(item.Value?.ToString()); } #pragma warning restore CA1031 // Do not catch general exception types } else { value = Convert.ChangeType(item.Value, prop.PropertyType); } } prop.SetValue(entity, value); } return(entity); }
public static T ConvertNullableFromString <T>(string param) { NullableConverter converter = new NullableConverter(typeof(T)); return((T)converter.ConvertFromString(param)); }
public static T?ConvertToNullable <T>(this object obj) where T : struct { var converter = new NullableConverter(typeof(T?)); return((T?)converter.ConvertFromString(obj.ToString())); }