//转换对象的成任何类型
//通用型
//效率不见得有多高
//当做特殊情况处理或者练习的话可以 public static object ChangeType(object value, Type type)
{
    if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
    if (value == null) return null;
    if (type == value.GetType()) return value;
    if (type.IsEnum)
    {
        if (value is string)
            return Enum.Parse(type, value as string);
        else
            return Enum.ToObject(type, value);
    }
    if (!type.IsInterface && type.IsGenericType)
    {
        Type innerType = type.GetGenericArguments()[];
        object innerValue = ChangeType(value, innerType);
        return Activator.CreateInstance(type, new object[] { innerValue });
    }
    if (value is string && type == typeof(Guid)) return new Guid(value as string);
    if (value is string && type == typeof(Version)) return new Version(value as string);
    if (!(value is IConvertible)) return value;
    return Convert.ChangeType(value, type);
}  //转载自  http://www.cnblogs.com/youring2/archive/2012/07/26/2610035.html

最新文章

  1. map 函数----filter函数
  2. C#之tcp自动更新程序
  3. oracle often commands
  4. quick-cocos2d-x之testlua之VisibleRect.lua
  5. [设计模式] Javascript 之 外观模式
  6. DEDECMS调用特定ID文章内容的实现方法
  7. 03-position和anchorPoint
  8. [.NET] 打造防“狼”神器 :任务栏篇
  9. 【html】【17】高级篇--loading加载
  10. my sql 实现批量操作及注意问题
  11. block代码块介绍
  12. iPhone图形开发绘图小结
  13. java提高篇(十一)-----代码块
  14. mysql基础篇-----mysql简介
  15. supermap数据库型数据源的数据索引问题
  16. POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
  17. 1024. Video Stitching
  18. C#异步编程のWPF Invoke和BeginInvoke
  19. DSO windowed optimization 代码 (4)
  20. jar is specified twice.

热门文章

  1. linux下mysql基础从安装到基本使用
  2. 【HDU 4311】Meeting point-1(前缀和求曼哈顿距离和)
  3. C++强制类型转换操作符 const_cast
  4. python协程和yeild
  5. 【caffe】mnist训练日志
  6. 【BZOJ-4326】运输计划 树链剖分 + 树上差分 + 二分
  7. 【BZOJ-2503】相框 并查集 + 分类讨论
  8. PHPCMS \phpcms\modules\member\index.php 用户登陆SQL注入漏洞分析
  9. CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)
  10. iOS自动处理键盘事件的第三方库:IQKeyboardManager