/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { if (context == null) { throw new ArgumentNullException("context"); } string[] ids = ((string)value).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List <object> items = new List <object>(); dynamic queryable = context.GetService(((EntityValueConverterContext)context).Property.Property.PropertyType.GetGenericArguments()[0]); for (int i = 0; i < ids.Length; i++) { Guid id; if (!Guid.TryParse(ids[i], out id)) { continue; } object item = queryable.GetEntity(id); if (item != null) { items.Add(item); } } return(items.ToArray()); }
/// <summary> /// Gets the satellite assembly for the specified culture. /// </summary> /// <param name="culture">The specified culture.</param> /// <returns>The specified satellite assembly.</returns> /// <exception cref="ArgumentNullException">culture is null.</exception> /// <exception cref="ArgumentException"></exception> public Assembly GetSatelliteAssembly(CultureInfo culture) { if (culture == null) { throw new ArgumentNullException("culture"); } Assembly assm = null; var baseName = FullName; string cultureName; while (assm == null && (cultureName = culture.Name) != "") { var assmName = baseName + "." + cultureName; assm = Load(assmName, false); culture = culture.Parent; } if (assm == null) { throw new ArgumentException(); // FIXME -- throw new FileNotFoundException(); } return(assm); }
public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { string s = value as string; if (s != null) { var margins = s.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (margins.Length == 1) { double uniformLength; if (double.TryParse(margins[0], out uniformLength)) { return(new Thickness(uniformLength)); } } if (margins.Length == 2) { double leftright, topbottom; if (double.TryParse(margins[0], out leftright) && double.TryParse(margins[1], out topbottom)) { return(new Thickness(leftright, topbottom, leftright, topbottom)); } } if (margins.Length == 4) { double left, right, top, bottom; if (double.TryParse(margins[0], out left) && double.TryParse(margins[1], out top) && double.TryParse(margins[2], out right) && double.TryParse(margins[3], out bottom)) { return(new Thickness(left, top, right, bottom)); } } } return(base.ConvertFrom(context, culture, value)); }
#pragma warning restore S4200 // Native methods should be wrapped /// <summary> /// Gets the satellite assembly for the specified culture. /// </summary> /// <param name="culture">The specified culture.</param> /// <returns>The specified satellite assembly.</returns> /// <exception cref="ArgumentNullException">culture is null.</exception> /// <exception cref="ArgumentException"></exception> public Assembly GetSatelliteAssembly(CultureInfo culture) { if (culture == null) { throw new ArgumentNullException("culture"); } Assembly assm = null; var baseName = FullName; string cultureName; while (assm == null && (cultureName = culture.Name) != "") { var assmName = baseName + "." + cultureName; assm = Load(assmName, false); culture = culture.Parent; } if (assm == null) { #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one throw new ArgumentException(); #pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one // FIXME -- throw new FileNotFoundException(); } return(assm); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { if (string.IsNullOrEmpty((string)value)) { return(null); } return(DateTime.Parse((string)value)); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The System.Object to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { if (!(value is IEntity)) { return(null); } return(((IEntity)value).ToString()); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The System.Object to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { if (value == null) { return(""); } return(((DateTime)value).ToLongDateString() + " " + ((DateTime)value).ToLongTimeString()); }
static void Main(string[] args) { System.Resources.ResourceManager rm = new Resources.ResourceManager(typeof(System.Resources_Example.Recurso)); System.Globalization.CultureInfo cultureInfo = new Globalization.CultureInfo("pt-BR"); string d = rm.GetString("DESCRICAO", cultureInfo); Console.WriteLine(d); Console.ReadKey(); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { Type type = ((EntityValueConverterContext)context).Property.Property.PropertyType; if (value is int) { return(Enum.ToObject(type, (int)value)); } return(Enum.Parse(type, (string)value)); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The System.Object to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { Type type = value.GetType(); if (type.GetCustomAttribute <FlagsAttribute>() != null) { Enum v = (Enum)value; Array values = Enum.GetValues(type); List <string> target = new List <string>(); foreach (Enum item in values) { if (!v.HasFlag(item)) { continue; } string name = Enum.GetName(type, item); FieldInfo field = type.GetField(name, Reflection.BindingFlags.Public | Reflection.BindingFlags.Static); DescriptionAttribute description = field.GetCustomAttribute <DescriptionAttribute>(); if (description != null) { target.Add(description.Description); continue; } DisplayAttribute display = field.GetCustomAttribute <DisplayAttribute>(); if (display != null) { target.Add(display.Name); } else { target.Add(name); } } return(string.Join(", ", target)); } else { string name = Enum.GetName(type, value); FieldInfo field = type.GetField(name, Reflection.BindingFlags.Public | Reflection.BindingFlags.Static); DescriptionAttribute description = field.GetCustomAttribute <DescriptionAttribute>(); if (description != null) { return(description.Description); } DisplayAttribute display = field.GetCustomAttribute <DisplayAttribute>(); if (display != null) { return(display.Name); } return(name); } }
public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType != null && value is ExtendedDateTimeInterval) { var instance = (ExtendedDateTimeInterval)value; if (destinationType == typeof(string)) { return(instance.ToString()); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { ModelItem modelItem = value as ModelItem; if (modelItem != null) { EditingContext editingContext = modelItem.GetEditingContext(); if (editingContext != null) { return(editingContext.Services.GetService <DesignerConfigurationService>().AnnotationEnabled); } } return(false); }
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { // I do use reflection for this. Another way would be to force the viewModel to implement sth like // IDragSource and IDropTarget. Then I could cast and return the method. Would probably be more // performant, but we force the viewmodel to reference an controls assembly. Lets try this code // until someone dont likes it. ;o) var mi = value.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance); if (mi == null) { throw new InvalidOperationException("Could not find method with name '" + methodName + "' on type '" + value.GetType() + "'."); } if (targetType == typeof(Func <object, bool>)) { return(new Func <object, bool>(x => (bool)mi.Invoke(value, new object[] { x }))); } else if (targetType == typeof(Func <int, object, bool>)) { return(new Func <int, object, bool>((i, x) => (bool)mi.Invoke(value, new object[] { i, x }))); } else if (targetType == typeof(Func <string, bool>)) { return(new Func <string, bool>(s => (bool)mi.Invoke(value, new object[] { s }))); } else if (targetType == typeof(Func <int, string, bool>)) { return(new Func <int, string, bool>((i, s) => (bool)mi.Invoke(value, new object[] { i, s }))); } else if (targetType == typeof(Action <object>)) { return(new Action <object>(x => mi.Invoke(value, new object[] { x }))); } else if (targetType == typeof(Action <int, object>)) { return(new Action <int, object>((i, x) => mi.Invoke(value, new object[] { i, x }))); } else if (targetType == typeof(Func <bool>)) { return(new Func <bool>(() => (bool)mi.Invoke(value, new object[] { }))); } else if (targetType == typeof(Func <object>)) { return(new Func <object>(() => mi.Invoke(value, null))); } return(null); }
public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { if (value == null) { throw GetConvertFromException(value); } var source = value as string; if (source != null) { return(ExtendedDateTime.Parse(source)); } return(base.ConvertFrom(context, culture, value)); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { if (context == null) { throw new ArgumentNullException("context"); } Guid id; if (!Guid.TryParse((string)value, out id)) { return(null); } dynamic queryable = context.GetService(((EntityValueConverterContext)context).Property.Property.PropertyType); return(queryable.GetEntity(id)); }
/// <summary> /// Determines whether we are currently in localization mode - i.e., language is not (Default). /// </summary> private bool CurrentlyLocalizing(IComponent rootComponent) { if (rootComponent != null) { PropertyDescriptor prop = TypeDescriptor.GetProperties(rootComponent)["Language"]; if (prop != null && prop.PropertyType == typeof(Globalization.CultureInfo)) { Globalization.CultureInfo ci = (Globalization.CultureInfo)prop.GetValue(rootComponent); if (!ci.Equals(Globalization.CultureInfo.InvariantCulture)) { return(true); } } } return(false); }
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { if (value is int) { return new Thickness { Left = (int)value } } ; if (value is double) { return new Thickness { Left = (double)value } } ; return(new Thickness()); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The System.Object to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { Type type = value.GetType(); string name = Enum.GetName(type, value); FieldInfo field = type.GetField(name, Reflection.BindingFlags.Public | Reflection.BindingFlags.Static); DescriptionAttribute description = field.GetCustomAttribute <DescriptionAttribute>(); if (description != null) { return(description.Description); } DisplayAttribute display = field.GetCustomAttribute <DisplayAttribute>(); if (display != null) { return(display.Name); } return(name); }
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { if (value == null) { return(string.Empty); } if (!value is DateTime) { throw new ArgumentException("The value's type has to be DateTime.", "value"); } DateTime dateTime = (DateTime)value; string returnValue = string.Empty; switch (DisplayStyle) { case Kind.Date: returnValue = dateTime.ToShortDateString(); break; case Kind.Time: returnValue = dateTime.ToLongTimeString(); break; case Kind.DateAndTime: returnValue = dateTime.ToString(); break; } if (!DisplaySeconds) { int index = returnValue.LastIndexOf(':'); if (index > -1) { returnValue = returnValue.Remove(index, 3); } } return(returnValue); }
public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { //The UriTypeConverter gives back a relative Uri for things like "www.microsoft.com". If //the Uri is relative, we'll try sticking "http://" on the front to see whether that fixes it up. Uri uri = base.ConvertFrom(context, culture, value) as Uri; if (uri != null && !string.IsNullOrEmpty(uri.OriginalString) && !uri.IsAbsoluteUri) { try { uri = new Uri("http://" + uri.OriginalString.Trim()); } catch (UriFormatException) { //We can't throw "http://" on the front: just return the original (relative) Uri, //which will throw an exception with reasonable text later. } } return(uri); }
public string Serialize(object value) { if (value == null) { return("null"); } Type t = value.GetType(); if (t == typeof(byte[])) { return(Convert.ToBase64String((byte[])value)); } if (t == typeof(DateTime)) { return(((DateTime)value).ToString("o", Globalization.CultureInfo.InvariantCulture.NumberFormat)); } Globalization.CultureInfo realCulture = Globalization.CultureInfo.CurrentCulture; Threading.Thread.CurrentThread.CurrentCulture = Globalization.CultureInfo.InvariantCulture; try { if (t.IsPrimitive || t == typeof(string) || t.IsValueType) { return(value.ToString()); } t = ObjectContext.GetObjectType(t); return(t.ToString()); } finally { Threading.Thread.CurrentThread.CurrentCulture = realCulture; } }
public static async Task <Regex> GetRegexAsync(RegexEngine engine, string pattern, RegexOptions options, Globalization.CultureInfo culture) { using (new System.Tests.ThreadCultureChange(culture)) { return(await GetRegexAsync(engine, pattern, options)); } }
public override object ConvertTo(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { return(((double)value).ToString()); }
public override object ConvertFrom(ITypeDescriptorContext context, Globalization.CultureInfo culture, object value) { return(double.Parse((string)value)); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The System.Object to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns>An System.Object that represents the converted value.</returns> public override object ConvertTo(ComponentModel.ITypeDescriptorContext context, Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return((bool)value ? "Male" : "Female"); } return(base.ConvertTo(context, culture, value, destinationType)); }
public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { throw new NotImplementedException(); }
object IValueConverter.Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { if (ReferenceEquals(value, null)) { return(Visibility.Collapsed); } var str = parameter as string; return(str == "IsNullOrWhiteSpace" ? ((value as string).IsNullOrWhiteSpace() ? Visibility.Collapsed : Visibility.Visible) : ((value as string).IsNullOrEmpty() ? Visibility.Collapsed : Visibility.Visible)); }
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { throw new NotSupportedException(); }
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { // TODO something like: return(((TimeSpan)value).ToString("hh\:mm")); }
public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture) { // TODO something like: return TimeSpan.ParseExact(value, "hh:\mm", CultureInfo.CurrentCulture) }