如何通过T的类型和Text值返回该类型的值例如:getValue<DateTime?>("2007-5-14") 返回日期类型的值;
getValue<int?>("5") 返回整形的值5; public static T getValue<T>(string strText)
{
T t1 = default(T); if (strText != "")
{
// 需要实现的代码
} return t1;
}
getValue<int?>("5") 返回整形的值5; public static T getValue<T>(string strText)
{
T t1 = default(T); if (strText != "")
{
// 需要实现的代码
} return t1;
}
解决方案 »
- .net新手、、 什么是 动态 语言?
- SOS......请大侠慷慨相助
- Counts = Convert.ToInt32(command.ExecuteScalar()) 提示确保参数格式正确 怎么改
- C# IP设置?
- dataGridView1控件简单问题
- c#求难题
- 在DataGrid修改数据后,如何保存至数据库
- 怎样在MDI子窗口中给父窗口中的菜单赋值?
- SQL SERVER中,在一个事务中保存大数据量(比如1000条)记录,提示“分布式事务已完成。请将此会话登记到新事务或 NULL 事务中。”,这是
- 在绑定的dropdownlist中能否加入一新项
- DataGrid隶属关系的显示 呕也 ~~~
- winform运行后,按回车,要在label1里显示"a",怎么写代码
return (T)Convert.ChangeType(strText,typof(T));
{
public static T GetValue<T>(string strText) //方法名规范应该大写开头
{
T t1 = default(T); if (strText != "")
{
return (T)Convert.ChangeType(strText, typeof(T));//楼上少一个e
} return t1;
} static void Main(string[] args)
{
DateTime d = GetValue<DateTime>("2007-5-14");
Console.WriteLine(d);
int i = GetValue<int>("5");
Console.WriteLine(i);
}
}