方法定义:

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. Shell 编程基础之基本语法结构汇总

    一.条件语句 简单条件 if [ condition ]; then # 当 condition 成立时,执行内容: fi # 将 if 反过来写,fi 结束 if 之意 复杂条件 if [ cond ...

  2. WPF之依赖属性

    Introduction When you begin to develop appliations with WPF, you will soon stumble across Dependency ...

  3. Optimizing Performance: Data Binding(zz)

    Optimizing Performance: Data Binding .NET Framework 4.5 Other Versions   Windows Presentation Founda ...

  4. HTML无刷新提交表单

    通常对于无刷新提交表单,我们都是运用ajax实现的.前段时间跟着老大了解到另一种无刷新提交表单的方法,是利用iframe框架实现的.现在整理出来分享给大家. 第一种: (html页面) <!DO ...

  5. 【BZOJ】3456: 城市规划

    http://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意:求n个点的无向连通图的方案.(n<=130000) #include <bi ...

  6. 【POJ】2151 Check the difficulty of problems

    http://poj.org/problem?id=2151 题意:T个队伍M条题目,给出每个队伍i的每题能ac的概率p[i][j],求所有队伍至少A掉1题且冠军至少A掉N题的概率(T<=100 ...

  7. FS_11C14温湿度传感器(二)

    作者:刘老师,华清远见嵌入式学院讲师. 在FS_11C14平台DHT11传感器程序: /******************************************************** ...

  8. javascript 时间操作

    javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=n ...

  9. kinderEditor + Struts2整合

    环境: kinderEditor4.1.5 Struts2.3.5 Spring3.0.5 Hibernate3.6 代码: FileManageAction package com.hcsoft.p ...

  10. #define is unsafe——I

    I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...