C#通用类型转换 Convert.ChangeType 转自网络
static public 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);
}
C#通用类型转换 Convert.ChangeType 转自网络的更多相关文章
- C#通用类型转换 Convert.ChangeType
]; object innerValue = ChangeType(value, innerType); return Activator.CreateInstance ...
- Convert.ChangeType不能处理Nullable类型的解决办法
在做一个ORMapping功能的时候发现,Convert.ChangeType不能处理nullable类型,比如int?. 解决办法也很简单,贴出完整的代码(大部分代码来自网络),注意下面代码没经过完 ...
- Convert.ChangeType转换泛型的性能损失测试
经常要传入参数包,当时一直是用泛型+ChangeType解决的.测试了下,看来这样确实慢了. 另外,可能都会认为Release发布之后会被优化掉.但测试了Release和Debug结果一样慢,比较失望 ...
- Convert.ChangeType不能处理Nullable类型的解决办法(转)
https://www.cnblogs.com/patrickyu/p/3211115.html 在做一个ORMapping功能的时候发现,Convert.ChangeType不能处理nullable ...
- python类型转换convert实例分析
在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo: 类型 说明 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 ...
- python开发_类型转换convert
在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo: int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float ...
- C# Convert.ChangeType()
Convert.ChangeType() 将未知类型转换为已知类型 ; object result = Convert.ChangeType(content, typeof(int)); 其他常用的转 ...
- SpringBoot 自定义参数类型转换convert
创建一个配置类.使用 @bean注入到容器中 @Bean public WebMvcConfigurer webMvcConfigurer(){ /** * 实现自定义的addConverter */ ...
- C#技巧记录——持续更新
作为一名非主修C#的程序员,在此记录下学习与工作中C#的有用内容,持续更新 对类型进行约束,class指定了类型必须是引用类型,new()指定了类型必须具有一个无参的构造函数 where T : cl ...
随机推荐
- 功能强大支持64位操作系统的转Flash软件(doc转swf):Print2Flash
Print2Flash是一个虚拟打印机类的文档转换软件,因此只要是可打印的文档,都可以轻松转换为Flash文件,即SWF动画,特别是用于转换PDF.Word.Excel.PowerPoint等文档为S ...
- SRM 451 DIV 1 总结
250p:这次是有史以来做的最快的一次250p...看题花了两分钟,敲代码最多一分钟...太明显了题意~ 500p:这题水了...每次都这样...很显然用DP来做,不过前面状态表示有问题了...搞了好 ...
- ASSER、VERIFY、TRACE详解
ASSERT()被测试它的参数,如果参数为零,则中断执行并打印一段说明消息.在Release版本的程序中它不起任何作用. ASSERT()使用的时候必须保证参数表达式中不能有函数调用,因此对于任何有函 ...
- Keil MDK AGDI Drivers, ULink, JLink, ST-Link, NuLink, JTAGjet
AGDI Drivers AGDI is an Application Program Interface (API) third-party developers can use to create ...
- maven中解决javax.servlet.jsp.PageContext cannot be resolved to a type
在eclipse环境下用maven出现:javax.servlet.jsp.PageContext cannot be resolved to a type. 这是由于没有引入jsp-api引发的问题 ...
- Python 数据类型
数据类型计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义 ...
- linux C 执行多个文件
- iOS开发——开发必备OC篇&UITableView设置界面完整封装(二)
UITableView设置界面完整封装(二) 简单MVC实现UITableView设置界面之Cell右边类型设置 首先来看看第一种方法证明使用,结合两种方法之后根据个人的爱好去选择就可以了, 一:使用 ...
- 云服务器 ECS Linux 修改编码格式
https://help.aliyun.com/knowledge_detail/41424.html?spm=5176.7841174.2.19.Le8kvy 通常情况下,云服务器 ECS Linu ...
- Xcode快照——管理应用程序版本
转自:http://blog.csdn.net/yuanbohx/article/details/8919474 1.创建快照:FIle → Create Snapshot 2.查看快照:Window ...