|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
convert an object to string and back (in a culture independent fashion)file. (as opposed to XmlSerializer written one, which crash sometimes, on some user's computer, for some unknown reason.. but I'm digressing) In this XML file I store object value, which I convert to string (when writting) and from string (when reading) with a couple of simple function which (should) do a reversible conversion (using TypeConverter) (functions below). Unfortunately, for some Enum, (namely System.Windows.Forms.Keys) I got a culture dependent string, even though I specify CultureInfo.InvariantCulture as an argument in my ConvertXXX method with the TypeConverter. Even using 'new CultureInfo("en")' do not fix this behavior. Any tip on how to fix these function (below) so they provide a truly culture independent reversible object=>string=>object mechanism? =================================================== public static bool TryGetStringValue<T>(object val, out string ret) { if (val is string) { ret = (string)val; return true; } TypeConverter tc = TypeDescriptor.GetConverter(typeof(T)); if (tc == null) { ret = val.ToString(); return false; } try { ret = tc.ConvertToString(null, CultureInfo.InvariantCulture, val); return true; } catch (ArgumentException) { } catch (NotSupportedException) { } ret = val.ToString(); return false; } public static bool TryGetTValue<T>(object val, out T tval) { if (val is T) { tval = (T)val; return true; } TypeConverter tc = TypeDescriptor.GetConverter(typeof(T)); if (tc == null) { tval = default(T); return false; } if (!tc.IsValid(val)) { tval = default(T); return false; } try { tval = (T)tc.ConvertFrom(null, CultureInfo.InvariantCulture, val); return true; } catch (ArgumentException) { } catch (NotSupportedException) { } tval = default(T); return false; } =================================================== If you look at KeysConverter in "reflector", and look at Initialize()
and ConvertTo(), you'll see that it gets the values from SR.GetString(), which calls ResourceManager.GetString passing a "null" culture, which in turn causes the system to use the CurrentUICulture. So; perhaps you could take a memento of the current UI culture, make your call and swap back? something like (untested, where Test2 does the conversion): static readonly CultureInfo fixedCulture = CultureInfo.InvariantCulture; static void Test() { CultureInfo memento = Thread.CurrentThread.CurrentUICulture; if (memento == fixedCulture) { Test2(); } else { try { Thread.CurrentThread.CurrentUICulture = fixedCulture; Test2(); } finally { Thread.CurrentThread.CurrentUICulture = memento; } } } Also - note that TypeConverter can also be specified against an individual property (not just a type), and the conversion can be dependent on the instance. You may or may not wish to consider using ITypeDescriptorContext to give this information to the converter. Marc Thanks for your answer....
I think I will try a special case for enum... Other converter can't be that stupid! Show quoteHide quote "Marc Gravell" <marc.grav***@gmail.com> a écrit dans le message de news:1178079690.112917.52800@n59g2000hsh.googlegroups.com... > If you look at KeysConverter in "reflector", and look at Initialize() > and ConvertTo(), you'll see that it gets the values from > SR.GetString(), which calls ResourceManager.GetString passing a "null" > culture, which in turn causes the system to use the CurrentUICulture. > > So; perhaps you could take a memento of the current UI culture, make > your call and swap back? something like (untested, where Test2 does > the conversion): > > static readonly CultureInfo fixedCulture = > CultureInfo.InvariantCulture; > static void Test() { > CultureInfo memento = > Thread.CurrentThread.CurrentUICulture; > if (memento == fixedCulture) { > Test2(); > } else { > try { > Thread.CurrentThread.CurrentUICulture = > fixedCulture; > Test2(); > } finally { > Thread.CurrentThread.CurrentUICulture = memento; > } > } > } > > Also - note that TypeConverter can also be specified against an > individual property (not just a type), and the conversion can be > dependent on the instance. You may or may not wish to consider using > ITypeDescriptorContext to give this information to the converter. > > Marc > thanks, it did work great!! ;-)
Show quoteHide quote "Marc Gravell" <marc.grav***@gmail.com> wrote in message news:%23E0LeSIjHHA.1900@TK2MSFTNGP04.phx.gbl... > For the special case, try dropping back to EnumConverter ;-p >
Other interesting topics
Locking Desktop and Threading Issue
The Interlocked on the Edge of Forever Determining if two paths are the same System.Speech Questions about clickOnce Truly cross platform? Windows Workflow - What basic tutorials? What's going on here? some config error happens periodically only Interface & Event Directory Sizes and Performance |
|||||||||||||||||||||||