DJP*_*JPB 57 c# generics reflection
我想将字符串转换为泛型类型
我有这个:
string inputValue = myTxtBox.Text;
PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName);
Type propType = propInfo.PropertyType;
object propValue = ?????
Run Code Online (Sandbox Code Playgroud)
我想将'inputString'转换为该属性的类型,以检查它是否兼容我该怎么做?
TKS
Lee*_*Lee 98
using System.ComponentModel;
TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);
Run Code Online (Sandbox Code Playgroud)
SWe*_*eko 14
object propvalue = Convert.ChangeType(inputValue, propType);
Run Code Online (Sandbox Code Playgroud)