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.Inv ariantCulture
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 TryGetStringVal ue<T>(object val, out string ret)
{
if (val is string)
{
ret = (string)val;
return true;
}
TypeConverter tc = TypeDescriptor. GetConverter(ty peof(T));
if (tc == null)
{
ret = val.ToString();
return false;
}
try
{
ret = tc.ConvertToStr ing(null, CultureInfo.Inv ariantCulture, val);
return true;
}
catch (ArgumentExcept ion) { }
catch (NotSupportedEx ception) { }
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(ty peof(T));
if (tc == null)
{
tval = default(T);
return false;
}
if (!tc.IsValid(va l))
{
tval = default(T);
return false;
}
try
{
tval = (T)tc.ConvertFr om(null, CultureInfo.Inv ariantCulture, val);
return true;
}
catch (ArgumentExcept ion) { }
catch (NotSupportedEx ception) { }
tval = default(T);
return false;
}
=============== =============== =============== ====== 4 3321
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 CurrentUICultur e.
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.Inv ariantCulture;
static void Test() {
CultureInfo memento =
Thread.CurrentT hread.CurrentUI Culture;
if (memento == fixedCulture) {
Test2();
} else {
try {
Thread.CurrentT hread.CurrentUI Culture =
fixedCulture;
Test2();
} finally {
Thread.CurrentT hread.CurrentUI Culture = 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
ITypeDescriptor Context 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**********@g mail.coma écrit dans le message de
news:11******** *************@n 59g2000hsh.goog legroups.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 CurrentUICultur e.
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.Inv ariantCulture;
static void Test() {
CultureInfo memento =
Thread.CurrentT hread.CurrentUI Culture;
if (memento == fixedCulture) {
Test2();
} else {
try {
Thread.CurrentT hread.CurrentUI Culture =
fixedCulture;
Test2();
} finally {
Thread.CurrentT hread.CurrentUI Culture = 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
ITypeDescriptor Context 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**********@g mail.comwrote in message
news:%2******** ********@TK2MSF TNGP04.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;
>select 0x616263;
|
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 to do a generic cast or
conversion on the type?
Here is sort of what I want to do:
object MyFunc(Type T, String Str)
{
object o;
|
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 do I convert it to:
dim myStr as string = "11,22,33,44"
| |
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?
-----------------------------------------------------------------------
Javascript variables are loosely typed: the conversion between a
string and a number happens automatically. Since plus (+) is also
used as in string concatenation, « '1' + 1 » is equal to « '11' »: the
String deciding...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------
Javascript variables are loosely typed: the conversion between a
string and a number happens automatically. Since plus (+) is also
used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the
String deciding...
|
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 dictionary
>>>from cStringIO import StringIO
>>> d={'a':'google', 'b':80, 'c':'string'}
>>> sio = StringIO()
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |