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

 

版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

http://www.cnblogs.com/cyq1162/archive/2010/04/18/1516958.html

相关文章
|
2月前
|
SQL
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
18 0
|
5月前
|
JavaScript
【vue】 国际化警告::[vue-i18n] Value of key ‘用户管理‘ is not a string
【vue】 国际化警告::[vue-i18n] Value of key ‘用户管理‘ is not a string
64 0
|
5月前
|
开发框架 .NET C#
C# Dictionary<string, string> 对key做筛选
C# Dictionary<string, string> 对key做筛选
62 2
|
5月前
|
JavaScript
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
67 1
|
JavaScript
vue 渲染列表报错Avoid using non-primitive value as key, use string/number value instead. found in
vue 渲染列表报错Avoid using non-primitive value as key, use string/number value instead. found in
66 0
|
人工智能
CF1660C Get an Even String(贪心)
CF1660C Get an Even String(贪心)
94 0
|
存储 缓存 监控
Redis学习2:课程安排、数据类型String操作及拓展操作和Key的设置约定
本系列文章是根据黑马程序员视频的学习笔记进行学习
Redis学习2:课程安排、数据类型String操作及拓展操作和Key的设置约定
|
前端开发 Java 编译器
|
存储 JSON NoSQL
string 类型使用注意事项与 key 的命名规范| 学习笔记
快速学习 string 类型使用注意事项与 key 的命名规范
268 0
jira项目笔记26-TS object 类型 和 {[key:string]: unknown} 定义类型的区别
jira项目笔记26-TS object 类型 和 {[key:string]: unknown} 定义类型的区别
181 0

热门文章

最新文章