zoukankan      html  css  js  c++  java
  • C#反射——模仿ParameterInterceptor(ashx处理程序)

    反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Reflection;
    
    namespace ReligionServer.util
    {
        public class ParametersUtil
        {
    
            private static NameValueCollection collection = null;
    
    
            //从Request.Param中获取指定的类型字段的值, 并用这些值结合字段初始化一个Hashtable  --> hashTable(String fieldName, String fieldName)fieldName来源于Request.Param中
            public static Hashtable GetHashtableFormRequest(HttpContext context, Type type)
            {
    
                collection = context.Request.Params;
    
                Hashtable map = new Hashtable();
                //FieldInfo[] fields = ReflectionUtil.GetFileldS(type); //在后来的修改之后, 返回List, 且包含当前类和除开Object的所有父类的属性
                String value = "";
                foreach (FieldInfo item in ReflectionUtil.GetFileldS(type))
                {
                    value = item.Name.Substring(item.Name.IndexOf("<") + 1, item.Name.IndexOf(">") - 1);
                    map.Add(value, collection.Get(value));
                }
                //System.Diagnostics.Debug.WriteLine(context.Request.Params.Count);
                //ClearForOnlyValueMap(map);//是否有必要
                return map;
            }
    
            /// <summary>
            /// 从HttpContext中初始化实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="context"></param>
            /// <param name="t"></param>
            /// <returns></returns>
            public static T GetInstanceFormRequest<T>(HttpContext context, T t)
            {
                String foreignKey = context.Request["ForeignKey"];
                collection = context.Request.Params;
                String value = "";
                foreach (FieldInfo item in ReflectionUtil.GetFileldS(t.GetType()))
                {
                    if (item.Name.IndexOf("<") != -1)
                    {
                        value = item.Name.Substring(item.Name.IndexOf("<") + 1, item.Name.IndexOf(">") - 1);
                    }
                    else
                    {
                        value = item.Name;
                    }
                    item.SetValue(t, GetValueByType(item.FieldType, collection.Get(value)));
                }
                return t;
            }
    
            public static Object GetValueByType(Type type, String value)
            {
                if (type.Equals(typeof(DateTime)))
                {
                    return DateUtil.ToDateTimeWithFormatf(value);
                }
                else if (type.Equals(typeof(MongoDB.Bson.BsonValue)))
                {
                    return MongoDB.Bson.BsonValue.Create(value);
                }
                else if (type.Equals(typeof(Boolean)))
                {
                    value = util.CommonUtil.IsEmpty(value) == true ? "false" : value;//如果上传参数为空, 那么默认初始化为false
                    return Boolean.Parse(value);
                } else if (type.Equals(typeof(int))) {
                    return Convert.ToInt32(value);
                }
                return value;
            }
    
            //将Hashtable中的空值去掉
            private static void ClearForOnlyValueMap(Hashtable map)
            {
                foreach (String key in map)
                {
                    if (null == map[key])
                    {
                        map.Remove(key);
                    }
                }
            }
    
        }
    }
  • 相关阅读:
    错删表空间的恢复步骤
    如何使用PL/SQL进行远程数据库连接
    Oracle基础笔记
    PL/SQL如何导入dmp文件
    oracle表的基本操作
    sql里面的分页
    truncate table语句和delete table语句的区别
    c++ 时间类型详解 time_t[转]
    C++ 容器:顺序性容器、关联式容器和容器适配器
    XCode 快捷键
  • 原文地址:https://www.cnblogs.com/threadj/p/10535909.html
Copyright © 2011-2022 走看看