转换为字符串/从字符串转换而不考虑文化 [英] Converting to/from a string without regard to culture

查看:51
本文介绍了转换为字符串/从字符串转换而不考虑文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有没有人知道如何以独立的方式将文件类型读取/写入

语言(文化)。例如,假设您使用原生的System.Drawing.Size处理

。在英文版Windows上键入。

您使用TypeConverter为此结构生成字符串,比如说,

" 50,75"然后将其存储在文件(或可能是数据库)中。现在,相同的

值可能会稍后从日本版本的

Windows中读回(或者更准确地说是在文化设置为<的线程上) br />
日语)。让我们说逗号(,)实际上是日语中的句号(。)

但是我只是为了演示目的而做这件事。你现在如何转换原始价值50,75和/或
进入尺寸给定的对象

系统可能不再识别逗号(只有一段时间)。

相反,你如何转换大小?对象返回与文化无关的

字符串(在本例中使用逗号),以便可以再次在

原始英文机器上处理它。或者也许这并不像我要的那样复杂。
让它成为现实。我只是不确定如何使用TypeConverter。 class

以一致的方式编写我的字符串,因为它必须在运行不同的CutlureInfo的线程上的

中读回。任何人都可以就此事提供任何

的见解。非常感谢。

Hi there,

Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let''s say you''re dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let''s say the comma (",") is actually a period (".") in Japanese
however though I''m just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won''t recognize the comma anymore (only a period).
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn''t as complicated as I''m
making it out to be. I''m just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.

推荐答案

有没有人知道如何在
中读取/写入类型的文件
Does anyone know how to go about reading/writing a type to a file in a

语言(文化)独立方式。例如,假设您使用原生的System.Drawing.Size处理

。输入英文版的

Windows。您使用TypeConverter为此结构生成

字符串,比如50,75,然后将其存储在一个文件(或者数据库)中。

现在,相同的值可能稍后会从这个文件中读回到

Japenese版本的Windows (或者更确切地说,在

文化被设置为日语的线程上)。让我们说日语中的逗号(,)实际上是一个

期间(。)但是我只是为了这个而做了这个。

演示目的。你现在如何将原始值50,75

转换为尺寸。因为系统可能不再认识

逗号(只有一段时间)。相反,如何将大小

对象转换回与文化无关的字符串(在本例中使用逗号)

以便可以对其进行处理原来的英文机器了。或者

也许这并不像我正在做的那么复杂。我只是没有

确定如何使用TypeConverter我必须在一个运行

并使用不同的CutlureInfo的线程上读回我的字符串,以一致的方式编写我的字符串

。任何人都可以提供有关

问题的任何见解。非常感谢。
language (culture) independent way. For instance, let''s say you''re dealing
with the native "System.Drawing.Size" type on an English version of
Windows. You use the "TypeConverter" for this structure to generate the
string, say, "50, 75" which you then store in a file (or perhaps a DB).
Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let''s say the comma (",") is actually a
period (".") in Japanese however though I''m just making this up for
demonstration purposes. How do you now convert the original value "50, 75"
into a "Size" object given that the system presumably won''t recognize the
comma anymore (only a period). Conversely, how do you convert a "Size"
object back to a culture-independent string (in this case using a comma)
so that it can be processed on the original English machine again. Or
maybe this isn''t as complicated as I''m making it out to be. I''m just not
sure how to use the "TypeConverter" class to write my string in a
consistent way given that it has to be read back in on a thread running
with a different "CutlureInfo". Can anyone provide any insight on the
matter. Thanks very much.



好​​吧,我想我需要打电话给

" TypeConverter.ConvertToInvariantString()"写入值和

" TypeConverter.ConvertFromInvariantString()"读回来。但是,

一旦使用后一个函数回读,我需要将值

传递给TypeConverter.ConvertToString()。如果我真的想要显示它(因为

我假设ConvertToString()是文化敏感的)。相反,如果用户在数据输入期间输入某个字段中的字符串,我需要使用TypeConverter.ConvertFromString()转换

。然后将其传递给

" TypeConverter.ConvertToInvariantString()"在再次将其写入文件之前。

这是正确的吗?谢谢。

Ok, I''m thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the value
to "TypeConverter.ConvertToString()" if I actually want to display it (since
I assume "ConvertToString()" is culture-sensitive). Conversely, if a user
enters the string in some field during data-input, I would need to convert
it using "TypeConverter.ConvertFromString()" and then pass this back to
"TypeConverter.ConvertToInvariantString()" before writing it to file again.
Is this correct? Thanks.


John Brown< no_spam@_nospam.comwrote:
John Brown <no_spam@_nospam.comwrote:

有谁知道如何去做以

语言(文化)独立方式读取/写入文件类型。例如,假设您使用原生的System.Drawing.Size处理

。在英文版Windows上键入。

您使用TypeConverter为此结构生成字符串,比如说,

" 50,75"然后将其存储在文件(或可能是数据库)中。现在,相同的

值可能会稍后从日本版本的

Windows中读回(或者更准确地说是在文化设置为<的线程上) br />
日语)。让我们说逗号(,)实际上是日语中的句号(。)

但是我只是为了演示目的而做这件事。你现在如何转换原始价值50,75和/或
进入尺寸给定的对象

系统可能不再识别逗号(只有一段时间)。
Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let''s say you''re dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let''s say the comma (",") is actually a period (".") in Japanese
however though I''m just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won''t recognize the comma anymore (only a period).



你测试过这个假设吗?我希望TypeConverter已经与文化无关,但我还没有尝试过。

Have you tested this assumption? I would hope that TypeConverter was
already culture-independent, but I haven''t tried it.


相反,你如何转换a尺寸对象返回与文化无关的

字符串(在本例中使用逗号),以便可以再次在

原始英文机器上处理它。或者也许这并不像我要的那样复杂。
让它成为现实。我只是不确定如何使用TypeConverter。 class

以一致的方式编写我的字符串,因为它必须在运行不同的CutlureInfo的线程上的

中读回。任何人都可以就此事提供任何

的见解。非常感谢。
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn''t as complicated as I''m
making it out to be. I''m just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.



我会说首先要做的是确保实际上有一个

问题。试试你认为会失败的情况。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

I''d say the first thing to do is to make sure there''s actually a
problem. Try exactly the situation you fear will fail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


你所说的逗号实际上是语言的一部分,C#,它是

a list separator,因此它与本地化无关!

" John Brown" < no_spam@_nospam.com写信息

新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
The comma you are talking about is actually part of the language, C#, it is
a list separator, and therefore it''s got nothing to do with localization!
"John Brown" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

>有没有人知道如何以独立的语言(文化)方式读取/写入文件类型。例如,假设您正在处理原生的System.Drawing.Size。键入Windows的英文版本。您使用TypeConverter对于这种结构来生成字符串,比如说50,75。然后将其存储在一个文件中(或者可能是一个
数据库)。现在,相同的值可能会在以后的日本版本的Windows上从此文件中读回(或者更准确地说是在
文化设置为日语的线程上)。让我们说逗号(,)实际上是日语中的句号(。),但是我只是为了演示目的而做这件事。你现在如何转换原始值50,
75进入尺寸因为系统可能不再识别逗号(只有一段时间)。相反,你如何转换
尺寸?将对象返回到与文化无关的字符串(在这种情况下使用逗号),以便可以在原始的英文机器上再次处理。
或许这并不像我那么复杂做到这一点。我只是不确定如何使用TypeConverter class,以一致的方式编写我的字符串,因为它必须在运行
与不同的CutlureInfo的线程上重新读回。任何人都可以对
事项提供任何见解。非常感谢。
>Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let''s say you''re
dealing with the native "System.Drawing.Size" type on an English version
of Windows. You use the "TypeConverter" for this structure to generate
the string, say, "50, 75" which you then store in a file (or perhaps a
DB). Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let''s say the comma (",") is actually a
period (".") in Japanese however though I''m just making this up for
demonstration purposes. How do you now convert the original value "50,
75" into a "Size" object given that the system presumably won''t recognize
the comma anymore (only a period). Conversely, how do you convert a
"Size" object back to a culture-independent string (in this case using a
comma) so that it can be processed on the original English machine again.
Or maybe this isn''t as complicated as I''m making it out to be. I''m just
not sure how to use the "TypeConverter" class to write my string in a
consistent way given that it has to be read back in on a thread running
with a different "CutlureInfo". Can anyone provide any insight on the
matter. Thanks very much.



好​​吧,我想我需要打电话给

" TypeConverter.ConvertToInvariantString()"写入值和

" TypeConverter.ConvertFromInvariantString()"读回来。但是,

一旦使用后一个函数回读,我需要将值

传递给TypeConverter.ConvertToString()。如果我真的想要显示它

(因为我假设ConvertToString()是文化敏感的)。相反,如果用户在数据输入期间在某些字段中输入字符串,我需要使用TypeConverter.ConvertFromString()来转换它。然后将此

传递回TypeConverter.ConvertToInvariantString()在再次将其写入

文件之前。它是否正确?谢谢。


Ok, I''m thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the value
to "TypeConverter.ConvertToString()" if I actually want to display it
(since I assume "ConvertToString()" is culture-sensitive). Conversely, if
a user enters the string in some field during data-input, I would need to
convert it using "TypeConverter.ConvertFromString()" and then pass this
back to "TypeConverter.ConvertToInvariantString()" before writing it to
file again. Is this correct? Thanks.



这篇关于转换为字符串/从字符串转换而不考虑文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆