如何从一个类型到的TryParse方法得到什么? [英] How do I get from a type to the TryParse method?
本文介绍了如何从一个类型到的TryParse方法得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的特殊问题:
我有一个字符串,它指定在配置类的aribitrary类型
I have a string which specifies an aribitrary type in a configuration class
Config.numberType =System.Foo;
其中,富
像十进制类型
或双击
我用 Type.GetType(Config.numberType)
返回相应的类型。
我如何从得到该类型能够使用, System.Foo.TryParse()
?
How do I get from that type to being able to use, System.Foo.TryParse()
?
一些进一步的相关查询
-
的TryParse()
可从访问System.Foo.TryParse()
以及foo.TryParse()
。这是否意味着富
在C#中的某种类的?这似乎不可思议,我认为INT
,双击
等,其实并不只是修改关键字。 - 你怎么能宣称在这种情况下变量? -
VAR
不是普遍使用的似乎也就是只在局部范围内等
TryParse()
can be accessed fromSystem.Foo.TryParse()
as well asfoo.TryParse()
. Does this meanfoo
is some kind of class in C#? This seems weird to me thatint
,double
etc are actually not just modifier keywords.- How can you declare variables under these circumstances? -
var
is not universally usable it seems i.e. only in local scope etc.
推荐答案
正如许多人所说 - 没有一个直接的途径。我想到一个比较接近的选项是的TypeConverter
:
As many have said - there isn't a direct route. I expect one close option is TypeConverter
:
Type type = typeof(double);
string text = "123.45";
object value = TypeDescriptor.GetConverter(type)
.ConvertFromInvariantString(text);
当然,你可能需要尝试
/ 抓
来处理异常。生活就是这样。
Of course, you may need try
/catch
to handle exceptions. Such is life.
这篇关于如何从一个类型到的TryParse方法得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文