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. Menu [D3D9 Source]

    源代码下载地址:http://download.csdn.net/detail/wd844125365_/8008779

  2. 用 Flask 来写个轻博客 (22) — 实现博客文章的添加和编辑页面

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 新建表单 新建视图函数 新建模板 在博客文章页面添加 New 和 Ed ...

  3. HBase最佳实践-读性能优化策略

    任何系统都会有各种各样的问题,有些是系统本身设计问题,有些却是使用姿势问题.HBase也一样,在真实生产线上大家或多或少都会遇到很多问题,有些是HBase还需要完善的,有些是我们确实对它了解太少.总结 ...

  4. MSF——基本使用和Exploit模块(一)

    MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) MSF——Metasploit Fra ...

  5. Ubuntu解压缩rar格式文件

    解压缩rar文件时,出现问题 解决方法: sudo apt-get install unrar

  6. 【题解】Antisymmetry

    题目大意 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是. 现在给出一个长度为N的 ...

  7. Nuget--基础连接已经关闭

    1.Nuget---基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系 修改一下 Package Source 改为 http://packages.nuget.org 2.Nuget- ...

  8. 字符串String的使用方法

    var ddd = "举头望明月,低头思故乡" document.writeln(ddd.split(''));//选择字符串中的一个标识符,将字符串分割成数组; var slic ...

  9. .360doc.com dot.net技术架构

  10. java switch语句注意事项

    /* switch语句的使用注意事项: 1.多个case后面的数据不可以重复 2.switch后面的小括号当中只能是下列数据类型: 基本数据类型:byte . short.char.int 引用数据类 ...