0
点赞
收藏
分享

微信扫一扫

public virtual T Get<T>(string key, T defaultValue)

新鲜小饼干 2022-08-15 阅读 84

这里以前写的草稿,今天就都发布了

这里一个放在页面基类PageBase中的用于获取参数的函数,简单好看。

页面中使用如下:

 

int id=Get<int>("id",-1);

string name=Get<string>("name","");

函数代码如下:
public virtual T Get<T>(string key, T defaultValue)
{
if (Request[key] == null) { return defaultValue; }
object result;
if (typeof(T).Name == "Int32")
{
int _result = 0;
if (!int.TryParse(Request[key], out _result))
{
return defaultValue;
}
result = _result;
}
else
{
result = Request[key].Trim();
}
return (T)result;
}

//只能把object类型转T,其它类型都报错

举报

相关推荐

0 条评论