如何将字符串转换为任何类型 [英] How to convert string to any type

查看:102
本文介绍了如何将字符串转换为任何类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为泛型



我有这个:

pre $ string inputValue = myTxtBox.Text;

PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName);
类型propType = propInfo.PropertyType;

对象propValue = ?????

我想将'inputString'转换为该属性的类型,以检查它是否兼容
我该怎么做?



tks

解决方案

 使用System.ComponentModel; 

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);


I want to convert a string to a generic type

I have this:

string inputValue = myTxtBox.Text;    

PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName);
Type propType = propInfo.PropertyType;

object propValue = ?????

I want to convert 'inputString' to the type of that property, to check if it's compatible how can I do that?

tks

解决方案

using System.ComponentModel;

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);

这篇关于如何将字符串转换为任何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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