• 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);
                    }
                }
            }
    
        }
    }
  • 相关阅读:
    linux 短信收发
    sama5d3 环境检测 adc测试
    【Codeforces 723C】Polycarp at the Radio 贪心
    【Codeforces 723B】Text Document Analysis 模拟
    【USACO 2.2】Preface Numbering (找规律)
    【Codeforces 722C】Destroying Array (数据结构、set)
    【USACO 2.1】Hamming Codes
    【USACO 2.1】Healthy Holsteins
    【USACO 2.1】Sorting A Three-Valued Sequence
    【USACO 2.1】Ordered Fractions
  • 原文地址:https://www.cnblogs.com/threadj/p/10535909.html
Copyright © 2020-2023  润新知