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);
                    }
                }
            }
    
        }
    }
  • 相关阅读:
    你认为做好测试计划工作的关键是什么?
    一套完整的测试应该由哪些阶段组成?
    你对测试最大的兴趣在哪里?为什么?
    如何测试一个纸杯?
    黑盒测试和白盒测试各自的优缺点
    在您以往的工作中,一条软件缺陷(或者叫Bug)记录都包含了哪些内容?如何提交高质量的软件缺陷(Bug)记录?
    测试人员在软件开发过程中的任务
    软件测试分为几个阶段? 各阶段的测试策略和要求是什么?
    软件测试的策略
    软件产品质量特性
  • 原文地址:https://www.cnblogs.com/threadj/p/10535909.html
Copyright © 2011-2022 走看看