例如:我有 2 个变量(值)和(属性)我想检查是否可以转换为值?我们不知道变量的类型,如何检查是否可以转换?
var value = Reader[item];
PropertyInfo property = properties.Where(x => x.Name == item).FirstOrDefault();
var type=property.PropertyType;//Or property.ReflectedType
var cs= value as type // Error: type is variable but is used like a Type
if (cs!=null){
...
}
样品 1:
var value = 123;//is int
type = property.GetType();// is double
var x = (double)value;//Can be casted
样本 2:
var value = "asd";//is string
type = property.GetType();// is double
var x = (double)value;//Can not be casted