当conversionType为可为null的int时如何使用Convert.ChangeType() [英] How to use Convert.ChangeType() when conversionType is a nullable int

查看:123
本文介绍了当conversionType为可为null的int时如何使用Convert.ChangeType()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,我要转换为:

I mean, I want to convert this:

string a = 24;
Convert.ChangeType(a, typeof(decimal?))

但是它让我错误。

更新1:

我有一个Type对象,可以是十进制? ,..许多可为null的类型。然后,使用Type对象,我需要转换类型对象中的字符串值。

I've got a Type object where can be decimal?, int?, .. many nullable types. Then with the Type object, I need to convert the string value in type object.

推荐答案

由于 Nullable< T> 不实现 IConvertable

string a = 24;
decimal? aAsDecimal = (decimal)Convert.ChangeType(a, typeof(decimal));

我也可能对 TryParse 感兴趣吗?

Might I also interest you in TryParse?

这篇关于当conversionType为可为null的int时如何使用Convert.ChangeType()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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