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;
}
================================================== = 4 3297
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!
--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best www.nova-mind.com
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de
news:11*********************@n59g2000hsh.googlegro ups.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
For the special case, try dropping back to EnumConverter ;-p
thanks, it did work great!! ;-)
--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best www.nova-mind.com
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
For the special case, try dropping back to EnumConverter ;-p This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark S Pryor |
last post by:
Hello,
Using MySQL 4.017 on Win2k sp3:
using MySQL built-ins: how can I convert a
value stored as a hexadecimal string back to
a binary string while logged into the shell?
>set @t1=616263;...
|
by: Ken Varn |
last post by:
I have an unknown numeric Type object passed into a function. I want to run
a conversion on a string to convert the string to that Type object and
return an object of that type. Is there some way...
|
by: Yifan |
last post by:
Hi
Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks
Yifan
|
by: Ronchese |
last post by:
Hi all.
Is possible I coerce a value to a determined type informed by a string? See
the sample:
dim obj as Object
obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")
|
by: moondaddy |
last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a
string, and then from a string back to a byte array.
for example
Private mByte() as New Byte(4){11,22,33,44}
Now how...
|
by: tienlx |
last post by:
Hi all,
I'm have a problem that i don't know how to convert object to char
in C# .
Does anybody know ?
Thanks.
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?...
|
by: neeru29 |
last post by:
I have new to python programming.
I have sucessfully converted a dictionary into a string.
but i want to know how can convert the string back to the dictionary.
let us say that 'd' is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |