zoukankan      html  css  js  c++  java
  • 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,其它类型都报错
    版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
    个人微信公众号
    创业QQ群:617713515
    Donation(扫码支持作者):支付宝:
    Donation(扫码支持作者):微信:
  • 相关阅读:
    Linux shell 学习总结
    linux shell 比较总结
    NSURL基本操作 HA
    Mac node.js install HA
    nodejs学习资料收集 HA
    xcode技巧 HA
    google web app/enxtions 学习资料收集 HA
    Failed to upload *.app on Device 可能的解决方法 HA
    iphone开发常见问题小集2 HA
    cocos2d收集 HA
  • 原文地址:https://www.cnblogs.com/cyq1162/p/1516958.html
Copyright © 2011-2022 走看看