你必须知道预期类型 如果你这样做你可以使用的TypeConverter如:
public object DetectType(string stringValue)
{
var expectedTypes = new List<Type> {typeof (DateTime), typeof (int)};
foreach (var type in expectedTypes)
{
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
try
{
// You'll have to think about localization here
object newValue = converter.ConvertFromInvariantString(stringValue);
if (newValue != null)
{
return newValue;
}
}
catch
{
// Can't convert given string to this type
continue;
}
}
}
return null;
}
大多数系统类型都有自己的类型转换器,你可以使用你的类的TypeConverter属性,并实现自己的转换器编写自己的
你需要写你自己....让我想知道你为什么需要那个...... – 2010-10-26 06:19:49