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

来源:互联网 发布:淘宝童装店标志设计 编辑:程序博客网 时间:2024/03/29 16:57

使用如下:

 

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,其它类型都报错
复制代码

原创粉丝点击