PC Review


Reply
Thread Tools Rate Thread

convert an object to string and back (in a culture independent fashion)

 
 
Lloyd Dupont
Guest
Posts: n/a
 
      2nd May 2007
to store user preference in our application we have an 'hand written' XML
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;
}
===================================================

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      2nd May 2007
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

 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      2nd May 2007
Thanks for your answer....
I think I will try a special case for enum...
Other converter can't be that stupid!

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
"Marc Gravell" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      2nd May 2007
For the special case, try dropping back to EnumConverter ;-p


 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      3rd May 2007
thanks, it did work great!! ;-)

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
"Marc Gravell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> For the special case, try dropping back to EnumConverter ;-p
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string to object Mat Child Microsoft Access Forms 2 10th Aug 2009 07:09 AM
convert an object to string and back (in a culture independent fashion) Lloyd Dupont Microsoft Dot NET Framework 4 3rd May 2007 12:26 AM
getting CLOB in provider independent fashion. an inquirer Microsoft ADO .NET 0 19th Sep 2006 04:32 PM
Connection String object Convert to String Variable Type =?Utf-8?B?TWlrZSBNb29yZQ==?= Microsoft ASP .NET 2 26th Oct 2004 03:43 PM
Convert a string to Ascii codes and then back to string again Kai Bohli Microsoft C# .NET 11 8th Jul 2004 03:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:10 AM.