方法定义:

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. HDU5853 Jong Hyok and String(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...

  2. CodeForceS#276-A

    A. Factory   One industrial factory is reforming working plan. The director suggested to set a mythi ...

  3. 微软曝光眼球追踪新专利,未来或将可以使用眼球控制HoloLens

    想要在增强现实(AR)和虚拟现实(VR)中获得感官能力,计算机的配合非常重要.如何能够更好的追踪我们所感知的,计算机判断用户目光所向的能力就必须进一步提高.根据微软新曝光的专利,可能已经有一个解决方案 ...

  4. html5 摇一摇事件监听

    先来看下html5的这几个特性: 1.deviceOrientation:方向传感器数据的事件,通过监听该事件可以获取手机静态状态下的方向数据: 2.deviceMotion: 运动传感器数据事件,通 ...

  5. Codeforces Round #210 (Div. 2) C. Levko and Array Recovery

    题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...

  6. Android -- 打开某个指定的网站

    1. 要使用的代码 Intent intent = new Intent(); intent.setData(Uri.parse(sUrl)); intent.setAction(Intent.ACT ...

  7. Linux 日常维护命令

    1   防火墙配置 1)重启后生效  开启: chkconfig iptables on  关闭: chkconfig iptables off 或者 /sbin/chkconfig --level  ...

  8. ios8 tableView设置滑动删除时 显示多个按钮

      ** *  tableView:editActionsForRowAtIndexPath:     //设置滑动删除时显示多个按钮 *  UITableViewRowAction          ...

  9. PHP 下的SSL加密设置

    这个是报的错[Composer\Downloader\TransportException] The . OpenSSL Error messages: error::SSL routines:SSL ...

  10. jquery插件之tab标签页或滑动门

    该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的tab标签页或滑动门特效,在此插件中默认使用的是鼠标滑过 ...