public virtual T Get(string key, T defaultValue)

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

这里一个放在页面基类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,其它类型都报错

转载于:https://my.oschina.net/secyaher/blog/274246

猜你喜欢

转载自blog.csdn.net/weixin_33860722/article/details/91967001
T