Generic TryParse

You should use the TypeDescriptor class:

public static T Convert<T>(this string input)
{
try
{
var converter = TypeDescriptor.GetConverter(typeof(T));
if(converter != null)
{
// Cast ConvertFromString(string text) : object to (T)
return (T)converter.ConvertFromString(input);
}
return default(T);
}
catch (NotSupportedException)
{
return default(T);
}
}

自己的版本

public static T GetAppSetting<T>(string key)
{
T result;
var value = ConfigurationManager.AppSettings[key];
if (string.IsNullOrEmpty(value))
{
throw new ConfigurationErrorsException($"Can not find app setting by key: {key}");
} Type type = typeof(T);
var converter = TypeDescriptor.GetConverter(type);
if (converter == null)
{
throw new ObjectNotFoundException($"Can not get the converter for Type: {type.FullName}");
} try
{
var obj = converter.ConvertFromString(value);
result = (T) obj;
}
catch (Exception ex)
{
throw new ConfigurationErrorsException(
$"Can not read the app setting by key: {key} with Type {type.FullName}", ex);
} return result;
}

通过泛型,将string转换为指定类型的更多相关文章

  1. 【转载】C#使用as关键字将对象转换为指定类型

    在C#的编程开发过程中,很多时候涉及到数据类型的转换,可使用强制转换的方式,不过强制转换数据类型有时候会抛出程序异常错误,可以使用as关键字来进行类型的转换,如果转换成功将返回转换后的对象,如果转换不 ...

  2. 如何实现@ResponseBody,把Json字符串转换为指定类型

    1.问题 spring 是如何把 http中的body,转换为指定类的,里面的难点其实在于泛型的处理. 2.Spring的处理 2.1 HandlerMethod 这个类Spring对Method的封 ...

  3. golang 学习之路 string转换为其他类型 其他类型转换为string

    将其他值转换为string 一般常用fmt.Sprintf(格式,转换的值) // 使用fmt.Sprintf 转换所有的类型为string 使用 这是第一种 // 注意在sprintf使用中需要注意 ...

  4. c# 将字符串转换为指定类型的值

    private object GetValueByProperty(string key, string value, ref Type typeValue) { Type t = typeof(T) ...

  5. C++中string转换为char*类型返回后乱码问题

    问题来源: 在写二叉树序列化与反序列化时发现序列化函数为char* Serialize1(TreeNode *root)  其函数返回类型为char*,但是我在实现的过程中为了更方便的操作添加字符串使 ...

  6. 将类型(int,string,…)转换为 T 类型

    方法定义: private static T GetValueByKey<T>(string key) where T : IConvertible { T localVal=defaul ...

  7. string转换为guid类型 split

    string str = "{"+context.Request["ID"]+"}"; KpiUser.ID = new Guid(str) ...

  8. 【枚举类型】Restful API请求--转换String为枚举类型

    IBaseEnum.java public interface IBaseEnum { public String getName(); } FuncEnum.java import com.sssl ...

  9. C++ String和其他类型互换

    在C++中如何实现String和其他类型互换呢?最好的方式是使用stringstream,下面简单介绍下: 1.其他类型转换为String #include <sstream> strin ...

随机推荐

  1. Win7系统取消登录界面的两种方法(图文)

    windows7系统设置电脑密码后,即使取消密码,也会出现登录界面 ,每次都要点击用户图标才能进入系统,这样比较麻烦.那么有什么办法可以取消登录界面呢?方法当然是有的,阅读下文教程,我们一起来看下Wi ...

  2. 2018-2019-2 20175213实验四 《Android开发基础》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:吕正宏 学号:20175213 指导教师:娄嘉鹏 实验日期:2019年5月14日 实验时间:13:45 - 21:00 实验序号:实验 ...

  3. CENTER OS7关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables. 所以在CentOS 7执行下面命令是无法查看防火墙状态的. [root@localhost ~]# ser ...

  4. 【awk】 判断是不是纯ascii串

    筛选出纯ascii串: awk '{ l = length($0); for (i = l; i > 0; i--) { if (substr($0,i,1) > "\177&q ...

  5. Uncaught (in promise) DOMException谷歌浏览器js报错分析

    Chrome的自动播放的政策在2018年4月做了更改,这点在开源中国的这篇文章中也有说到. 新的行为:浏览器为了提高用户体验,减少数据消耗,现在都在遵循autoplay政策,Chrome的autopl ...

  6. Vue是如何渲染页面的,渲染过程以及原理代码

    Vue是如何渲染页面的,渲染过程以及原理代码:https://www.cnblogs.com/ypinchina/p/7238402.html

  7. 创建网关项目(Spring Cloud Gateway)

    创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <s ...

  8. Oracle学习笔记<4>

    多表查询 1.什么是多表查询? 一次select语句需要查询的内容来自于不止一张表. 同时从多张表中查询数据. 单表查询: select id,last_name,salary from s_emp ...

  9. c# 自定义控件之 ComboBox

    winform 自带的 combobox 无法支持根据输入文本匹配列表中的项目,需要使用自定义控件. public class MyCombobox : ComboBox { //初始化数据项 pri ...

  10. 记一些经常用到的linux命令

    记一些经常用到的linux命令,备忘用 用清华源pip: pip install django==1.11  tensorflow==1.4.0 keras==2.0.6 -i https://pyp ...