方法定义:

private static T GetValueByKey<T>(string key) where T : IConvertible
{
T localVal=default(T);

string strType = typeof(T).Name;
string valuesData = ConfigurationManager.AppSettings[key].ToString();

localVal = (T)Convert.ChangeType(valuesData, typeof(T));

return localVal;

}

public static TConvertType DoConvert<TConvertType>(object convertValue, out bool hasConverted)
{
hasConverted = false;
var converted = default(TConvertType);
try
{
converted = (TConvertType)
Convert.ChangeType(convertValue, typeof(TConvertType));
hasConverted = true;
}
catch (InvalidCastException)
{
}
catch (ArgumentNullException)
{
}
catch (FormatException)
{
}
catch (OverflowException)
{
} return converted;
} 写法二:

public static TConvertType DoConvert<TConvertType>(object convertValue, out bool hasConverted)
{
hasConverted = false;
var converted = default(TConvertType);
try
{
converted = (TConvertType)
Convert.ChangeType(convertValue, typeof(TConvertType));
hasConverted = true;
}
catch (InvalidCastException)
{
}
catch (ArgumentNullException)
{
}
catch (FormatException)
{
}
catch (OverflowException)
{
}

return converted;
}

 

 

调用:

GetValueByKey<string>("aaa");

GetValueByKey<int>("bbb");

参考:http://stackoverflow.com/questions/8171412/cannot-implicitly-convert-type-int-to-t

将类型(int,string,…)转换为 T 类型的更多相关文章

  1. javaScript中其他类型的值转换为Boolean类型

    将javaScript中其他任意类型的值转换为对应Boolean类型的值. 一  将number类型的值转换为Boolean类型 数值为0: var myBoolean = new Boolean(0 ...

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

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

  3. golang interface类型转string等其他类型

    inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似

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

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

  5. string转换为guid类型 split

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

  6. 通过泛型,将string转换为指定类型

    Generic TryParse You should use the TypeDescriptor class: public static T Convert<T>(this stri ...

  7. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  8. 将list列表中unicode类型的值转换为字符串类型

  9. 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”

    错误的写法: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式也可以是别 ...

随机推荐

  1. 【原】MAC显示隐藏文件夹命令

    显示隐藏文件夹 1. 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 第一步:命令行执行上述命令:

  2. (转)Storm UI 解释

    Storm UI link:http://lbxc.iteye.com/category/221265 本文主要解释下storm ui上各项属性的含义. 1. mainpage 首页主要分为3块: a ...

  3. codeforces round #234B(DIV2) C Inna and Huge Candy Matrix

    #include <iostream> #include <vector> #include <algorithm> #include <utility> ...

  4. 《深入浅出Windows 10通用应用开发》

        <深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...

  5. box-sizing的相关属性

    box-sizing有三个属性,分别是:content-box,border-box,inherit (1)content-box:在宽度和高度之外绘制元素的内边距和边框(默认属性) (2)borde ...

  6. xml学习

    一,数据类型 xmlChar  对char的基本代替,是一个UTF-8编码字符串中的一个字节.如果你的数据使用了其他编码,在使用libxml函数前就必须转换为UTF-8. xmlDoc和xmlDocP ...

  7. CentOS评估磁盘I/O性能读写极限测试

    用一个fio工具 安装 yum -y install fio 二,FIO用法: 随机读:fio  -direct=1 -iodepth 1 -thread -rw=randread -ioengine ...

  8. phpMyAdmin:无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装。

    一:错误提示 英文:Cannot start session without errors, please check errors given in your PHP and/or webserve ...

  9. 将普通工程转为mvn标准工程(main resources)

    It is sometimes required to change the default source folder working on the java project. One best e ...

  10. HDU 2546

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...