Exemple #1
0
        /// <summary>
        /// 把集合转换成字典类
        /// </summary>
        /// <typeparam name="TKey">键类型</typeparam>
        /// <typeparam name="TValue">值类型 </typeparam>
        /// <param name="collection">集合类</param>
        /// <param name="keyProperty">键名</param>
        /// <returns></returns>
        public static Dictionary <TKey, TValue> ListToDictionary <TKey, TValue>(IEnumerable collection, string keyProperty)
        {
            Dictionary <TKey, TValue> dic = new Dictionary <TKey, TValue>();

            FastPropertyHandler handle = FastValueGetSet.GetGetMethodInfo(keyProperty, typeof(TValue));

            object[] emptyParams = new object[] { };
            foreach (TValue objValue in collection)
            {
                object obj = objValue;
                if (obj == null)
                {
                    continue;
                }
                TKey   key    = default(TKey);
                object objKey = handle(obj, emptyParams);
                if (typeof(TKey) == DefaultType.StringType)
                {
                    object strkey = objKey.ToString();
                    dic[(TKey)strkey] = objValue;
                }
                else
                {
                    key      = (TKey)objKey;
                    dic[key] = objValue;
                }
            }
            return(dic);
        }
 /// <summary>
 /// 创建属性的信息类
 /// </summary>
 /// <param name="getHandle">get委托</param>
 /// <param name="setHandle">set委托</param>
 /// <param name="propertyType">属性数据类型</param>
 public PropertyInfoHandle(Type belong, FastPropertyHandler getHandle, FastPropertyHandler setHandle, Type propertyType, string propertyName)
 {
     this._getHandle    = getHandle;
     this._setHandle    = setHandle;
     this._propertyType = propertyType;
     this._propertyName = propertyName;
     _belong            = belong;
 }
Exemple #3
0
        /// <summary>
        /// 获取属性的信息(不使用缓存)
        /// </summary>
        /// <param name="proName">属性名</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static PropertyInfoHandle GetPropertyInfoHandleWithOutCache(string proName, Type type)
        {
            FastPropertyHandler getHandle = GetGetMethodInfo(proName, type);
            FastPropertyHandler setHandle = GetSetMethodInfo(proName, type);
            PropertyInfo        pinf      = type.GetProperty(proName, AllBindingFlags);//获取子元素集合的属性
            Type proType = null;

            if (pinf != null)
            {
                proType = pinf.PropertyType;
            }
            PropertyInfoHandle propertyHandle = new PropertyInfoHandle(type, getHandle, setHandle, proType, proName);

            return(propertyHandle);
        }
Exemple #4
0
        /// <summary>
        /// 获取设置值的方法接口
        /// </summary>
        /// <param name="proName">属性名</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static FastPropertyHandler GetSetMethodInfo(string proName, Type type)
        {
            MethodInfo methodInfo = type.GetMethod("set_" + proName, AllBindingFlags);

            if (methodInfo == null)
            {
                return(null);
            }
            else if (methodInfo.GetParameters().Length != 1)
            {
                return(null);
            }
            FastPropertyHandler fastInvoker = FastPropertyInvoke.GetMethodInvoker(methodInfo);

            return(fastInvoker);
        }
Exemple #5
0
        /// <summary>
        /// 初始化类型的属性信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>如果已经初始化过侧返回false</returns>
        private static void InitClassPropertyInfos(Type type)
        {
            string fullName = type.FullName;

            //实例化本类型的句柄
            CreateInstanceHandler createrHandel = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type);
            Dictionary <string, PropertyInfoHandle> dicPropertys = new Dictionary <string, PropertyInfoHandle>();
            Dictionary <string, FieldInfoHandle>    dicField     = new Dictionary <string, FieldInfoHandle>();

            //属性信息句柄
            PropertyInfo[] destproper = type.GetProperties(FastValueGetSet.AllBindingFlags);
            FieldInfo[]    allField   = type.GetFields(FastValueGetSet.AllBindingFlags);
            //int index = 0;
            ///读取属性别名
            foreach (PropertyInfo pinf in destproper)
            {
                ///通过属性来反射
                string proName = pinf.Name;

                FastPropertyHandler getHandle = FastValueGetSet.GetGetMethodInfo(proName, type);
                FastPropertyHandler setHandle = FastValueGetSet.GetSetMethodInfo(proName, type);
                if (getHandle != null || setHandle != null)
                {
                    PropertyInfoHandle classProperty = new PropertyInfoHandle(type, getHandle, setHandle, pinf.PropertyType, pinf.Name);
                    dicPropertys.Add(pinf.Name, classProperty);
                }
            }

            ///读取属性别名
            foreach (FieldInfo fInf in allField)
            {
                string proName = fInf.Name;

                GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(fInf);
                SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(fInf);
                if (getHandle != null || setHandle != null)
                {
                    FieldInfoHandle fieldInfo = new FieldInfoHandle(type, getHandle, setHandle, fInf.FieldType, fInf.Name, fInf);
                    dicField.Add(fInf.Name, fieldInfo);
                }
            }


            ClassInfoHandle classInfo = new ClassInfoHandle(type, createrHandel, dicPropertys, dicField);

            dicClass.Add(fullName, classInfo);
        }
        /// <summary>
        /// 生成调用方法
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <returns></returns>
        public static FastPropertyHandler GetMethodInvoker(MethodInfo methodInfo)
        {
            DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object), typeof(object) }, methodInfo.DeclaringType.Module);
            ILGenerator   il            = dynamicMethod.GetILGenerator();

            ParameterInfo[] ps = methodInfo.GetParameters();
            //LocalBuilder ret=il.DeclareLocal(typeof(object));


            if (!methodInfo.IsStatic)
            {
                il.Emit(OpCodes.Ldarg_0);
            }
            for (int i = 0; i < ps.Length; i++)
            {
                il.Emit(OpCodes.Ldarg_S, (i + 1));//加载第几个参数
                FastInvoke.EmitCastToReference(il, ps[i].ParameterType);
            }
            if (methodInfo.IsStatic)
            {
                il.Emit(OpCodes.Call, methodInfo);
            }
            else
            {
                il.Emit(OpCodes.Callvirt, methodInfo);
            }
            Type retType = methodInfo.ReturnType;

            if (retType == FastInvoke.VoidType)
            {
                il.Emit(OpCodes.Ldnull);
            }
            else
            {
                FastInvoke.EmitBoxIfNeeded(il, retType);
            }
            il.Emit(OpCodes.Ret);
            FastPropertyHandler invoder = (FastPropertyHandler)dynamicMethod.CreateDelegate(typeof(FastPropertyHandler));

            return(invoder);
        }