TypeUtils -- Object 转为 强类型
2016-01-28 11:48
381 查看
public static class TypeUtils
{
/// <summary>
/// Object 转为 强类型
/// </summary>
public static T ConvertType<T>(object value)
{
if (value == null)
return default(T);
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
if (typeConverter.CanConvertFrom(value.GetType()))
{
return (T)typeConverter.ConvertFrom(value);
}
typeConverter = TypeDescriptor.GetConverter(value.GetType());
if (typeConverter.CanConvertTo(typeof(T)))
{
return (T)typeConverter.ConvertTo(value, typeof(T));
}
return (T)Convert.ChangeType(value, typeof(T));
}
}
{
/// <summary>
/// Object 转为 强类型
/// </summary>
public static T ConvertType<T>(object value)
{
if (value == null)
return default(T);
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
if (typeConverter.CanConvertFrom(value.GetType()))
{
return (T)typeConverter.ConvertFrom(value);
}
typeConverter = TypeDescriptor.GetConverter(value.GetType());
if (typeConverter.CanConvertTo(typeof(T)))
{
return (T)typeConverter.ConvertTo(value, typeof(T));
}
return (T)Convert.ChangeType(value, typeof(T));
}
}
相关文章推荐
- iOS开发-Objective-C与JavaScript交互
- Objective-C中类和方法的定义以及协议的使用
- Animation动画详解(七)——ObjectAnimator基本使用
- Objective-C Runtime 运行时与类的内部构造
- Bootstrap 多媒体对象(Media Object)
- bean和json相互转换工具类(工具ObjectMapper)
- Object-C 类,对象,运行时,isa 附带类本质的内存图
- 020: class, objects and instance: 一个简单的例子,压缩文件中内容的替换
- Selenium2(java)页面对象模型(Page Object) 八
- Objective-C与JavaScript交互的那些事
- RemObjects SDK 9.0 Beta_CodeFirst
- Objective-C 通知
- Error: while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
- Objective-C 单例
- JSON对象中的JSONObject和JSONArray以及与Map、String、数组的转化
- Objective-C特性:Runtime
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was ...
- python object类
- 对 removeObject 的理解
- node之object-assign