public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value != null) { if (BaseType == typeof(double)) { return(DoubleToString((double)value, Format, GetCulture(culture).NumberFormat)); } else if (BaseType == typeof(decimal)) { return(DecimalToString((decimal)value, Format, GetCulture(culture).NumberFormat)); } else if (BaseType == typeof(float)) { return(FloatToString((float)value, Format, GetCulture(culture).NumberFormat)); } else if (BaseType == typeof(int)) { return(IntToString((int)value, Format, GetCulture(culture).NumberFormat)); } else { return(m_BaseTypeConverter.ConvertTo(context, culture, value, destinationType)); } } else { return(m_BaseTypeConverter.ConvertTo(context, culture, value, destinationType)); } }
public void setValue(iModel model, object val) { // parse Enum values ... if (Type.IsEnum) { if (DbType == System.Data.DbType.String) { _fieldInfo.SetValue(model, Enum.Parse(Type, val.ToString())); } else { _fieldInfo.SetValue(model, Enum.ToObject(Type, val)); } } else if (Type == typeof(bool) && DbType == System.Data.DbType.String) { _fieldInfo.SetValue(model, bool.Parse(val.ToString())); } else if (val != null && val.Equals(DBNull.Value) && Type.IsValueType) { _fieldInfo.SetValue(model, Activator.CreateInstance(Type)); } else if (val != null && !Type.Equals(val.GetType())) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(val.GetType()); _fieldInfo.SetValue(model, converter.ConvertTo(val, Type)); } else { _fieldInfo.SetValue(model, val); } }
public static T[] ConvertTo <T>(this Array ar) { T[] ret = new T[ar.Length]; System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); if (tc.CanConvertFrom(ar.GetValue(0).GetType())) { for (int i = 0; i < ar.Length; i++) { ret[i] = (T)tc.ConvertFrom(ar.GetValue(i)); } } else { tc = System.ComponentModel.TypeDescriptor.GetConverter(ar.GetValue(0).GetType()); if (tc.CanConvertTo(typeof(T))) { for (int i = 0; i < ar.Length; i++) { ret[i] = (T)tc.ConvertTo(ar.GetValue(i), typeof(T)); } } else { throw new NotSupportedException(); } } return(ret); }
//------------------------------------------------------ // // Internal Methods // //------------------------------------------------------ #region Internal Methods // Returns non-null string if this value can be converted to a string, // null otherwise. internal static string GetStringValue(DependencyProperty property, object propertyValue) { string stringValue = null; // Special cases working around incorrectly implemented type converters if (property == UIElement.BitmapEffectProperty) { return(null); // Always treat BitmapEffects as complex value } if (property == Inline.TextDecorationsProperty) { stringValue = TextDecorationsFixup((TextDecorationCollection)propertyValue); } else if (typeof(CultureInfo).IsAssignableFrom(property.PropertyType)) //NumberSubstitution.CultureOverrideProperty { stringValue = CultureInfoFixup(property, (CultureInfo)propertyValue); } if (stringValue == null) { DPTypeDescriptorContext context = new DPTypeDescriptorContext(property, propertyValue); System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(property.PropertyType); Invariant.Assert(typeConverter != null); if (typeConverter.CanConvertTo(context, typeof(string))) { stringValue = (string)typeConverter.ConvertTo( context, System.Globalization.CultureInfo.InvariantCulture, propertyValue, typeof(string)); } } return(stringValue); }
private static string GetPrintableText(TSgrdDataGrid AGrid, int AGridColumnId, object ADataColumn) { string ReturnValue; EditorBase editor = AGrid.Columns[AGridColumnId].DataCell.Editor; if (editor != null) { System.ComponentModel.TypeConverter tc = editor.TypeConverter; if (tc is TSgrdDataGrid.PartnerKeyConverter || tc is TypeConverter.TDecimalConverter || tc is TypeConverter.TCurrencyConverter || tc is TypeConverter.TDateConverter || tc is TypeConverter.TShortTimeConverter || tc is TypeConverter.TLongTimeConverter) { ReturnValue = (string)tc.ConvertTo(ADataColumn, typeof(System.String)); } else { // Including Boolean - in a grid that is a checkbox but we want simple text ReturnValue = ADataColumn.ToString(); } } else { // Simple cell with no special editor ReturnValue = ADataColumn.ToString(); } return(ReturnValue); }
public override void Register(RegistrationContext context) { using (Key key = context.CreateKey(EditorRegKey)) { key.SetValue(string.Empty, FactoryType.Name); key.SetValue("DisplayName", string.Format(CultureInfo.InvariantCulture, "#{0}", NameResourceID)); key.SetValue("Package", context.ComponentType.GUID.ToString("B")); key.SetValue("CommonPhysicalViewAttributes", (int)_commonPhysicalViewAttributes); key.SetValue("EditorTrustLevel", (int)_trustLevel); using (Key key2 = key.CreateSubkey("LogicalViews")) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(LogicalView)); object[] customAttributes = FactoryType.GetCustomAttributes(typeof(ProvideViewAttribute), true); foreach (ProvideViewAttribute attribute in customAttributes) { if (attribute.LogicalView != LogicalView.Primary) { Guid guid = (Guid)converter.ConvertTo(attribute.LogicalView, typeof(Guid)); string physicalView = attribute.PhysicalView ?? string.Empty; key2.SetValue(guid.ToString("B"), physicalView); } } } } }
public static bool _ConvertTo_System_ComponentModel_TypeConverter_System_ComponentModel_ITypeDescriptorContext_System_Globalization_CultureInfo_System_Object_System_Type( ) { //class object System.ComponentModel.TypeConverter _System_ComponentModel_TypeConverter = new System.ComponentModel.TypeConverter(); //Parameters System.ComponentModel.ITypeDescriptorContext context = null; System.Globalization.CultureInfo culture = null; System.Object _value = null; System.Type destinationType = null; //ReturnType/Value System.Object returnVal_Real = null; System.Object returnVal_Intercepted = null; //Exception System.Exception exception_Real = null; System.Exception exception_Intercepted = null; InterceptionMaintenance.disableInterception( ); try { returnVal_Real = _System_ComponentModel_TypeConverter.ConvertTo(context, culture, _value, destinationType); } catch (System.Exception e) { exception_Real = e; } InterceptionMaintenance.enableInterception( ); try { returnVal_Intercepted = _System_ComponentModel_TypeConverter.ConvertTo(context, culture, _value, destinationType); } catch (System.Exception e) { exception_Intercepted = e; } return((exception_Real.Messsage == exception_Intercepted.Message) && (returnValue_Real == returnValue_Intercepted)); }
public static bool _ConvertTo_System_ComponentModel_TypeConverter_System_ComponentModel_ITypeDescriptorContext_System_Globalization_CultureInfo_System_Object_System_Type( ) { //class object System.ComponentModel.TypeConverter _System_ComponentModel_TypeConverter = new System.ComponentModel.TypeConverter(); //Parameters System.ComponentModel.ITypeDescriptorContext context = null; System.Globalization.CultureInfo culture = null; System.Object _value = null; System.Type destinationType = null; //ReturnType/Value System.Object returnVal_Real = null; System.Object returnVal_Intercepted = null; //Exception System.Exception exception_Real = null; System.Exception exception_Intercepted = null; InterceptionMaintenance.disableInterception( ); try { returnVal_Real = _System_ComponentModel_TypeConverter.ConvertTo(context,culture,_value,destinationType); } catch( System.Exception e ) { exception_Real = e; } InterceptionMaintenance.enableInterception( ); try { returnVal_Intercepted = _System_ComponentModel_TypeConverter.ConvertTo(context,culture,_value,destinationType); } catch( System.Exception e ) { exception_Intercepted = e; } return( ( exception_Real.Messsage == exception_Intercepted.Message ) && ( returnValue_Real == returnValue_Intercepted ) ); }
private static object _ChangeType(object obj, System.Type type) { System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(obj); if (((null == typeConverter) || (false == typeConverter.CanConvertTo(type)))) { return(System.Convert.ChangeType(obj, type)); } return(typeConverter.ConvertTo(obj, type)); }
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value != null) { return(((DateTime)value).ToString(m_Format, GetCulture(culture).DateTimeFormat)); } else { return(m_BaseTypeConverter.ConvertTo(context, culture, value, destinationType)); } }
/// <summary> /// Gets byte[] from a bitmap. /// </summary> /// <param name="bitmap">Bitmap to convert.</param> /// <returns>Byte[].</returns> public static byte[] BytesFromBitmap(Bitmap bitmap) { try { System.ComponentModel.TypeConverter bitmapConverter = System.ComponentModel.TypeDescriptor.GetConverter(bitmap.GetType()); return((byte[])bitmapConverter.ConvertTo(bitmap, typeof(byte[]))); } catch { throw new ApplicationException(); } }
internal static TT GenericConvert <FT, TT>(FT value) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(TT)); if (converter.CanConvertFrom(typeof(FT))) { return((TT)converter.ConvertFrom(value)); } converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(FT)); if (converter.CanConvertTo(typeof(TT))) { return((TT)converter.ConvertTo(value, typeof(TT))); } throw new FormatException(string.Format("Cannot convert from type: \"{0}\" to type: \"{1}\"", typeof(FT).Name, typeof(TT).Name)); }
internal static TT GenericConvert <FT, TT>(FT value, TT defaultValue) { if (value == null) { return(defaultValue); } System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(TT)); if (converter.CanConvertFrom(typeof(FT))) { return((TT)converter.ConvertFrom(value)); } converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(FT)); if (converter.CanConvertTo(typeof(TT))) { return((TT)converter.ConvertTo(value, typeof(TT))); } return(defaultValue); }
//---------------------------------Implementation-----------------------------// //var texto = "123123"; //var res = texto.ConvertTo<int>(); //res++; //---------------------------------Implementation-----------------------------// /// <summary> /// Converts to a specific data type. /// </summary> /// <typeparam name="TValue">The type of the t value.</typeparam> /// <param name="text">The text.</param> /// <returns>TValue.</returns> /// <exception cref="System.NotSupportedException"></exception> public static TValue ConvertTo <TValue>(this string text) { TValue res = default(TValue); System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(TValue)); if (tc.CanConvertFrom(text.GetType())) { res = (TValue)tc.ConvertFrom(text); } else { tc = System.ComponentModel.TypeDescriptor.GetConverter(text.GetType()); if (tc.CanConvertTo(typeof(TValue))) { res = (TValue)tc.ConvertTo(text, typeof(TValue)); } else { throw new NotSupportedException(); } } return(res); }