Exemplo n.º 1
0
        //获取一个设置特定字段的值的委托
        /// <summary>
        /// 获取一个设置特定字段的值的委托
        /// <para>返回结果:
        /// </para>一个void ReflectSet(object obj, object value)类型的委托
        /// </summary>
        /// <param name="classType">需要设置字段的类</param>
        /// <param name="fieldName">字段名称</param>
        public static ReflectSet SetFieldValue(Type classType, string fieldName)
        {
            ReflectSet returnDelegate;

            string cacheKey = classType.FullName + SPACE + fieldName;

            if (cacheSet.TryGetValue(cacheKey, out returnDelegate))
            {
                return(returnDelegate);
            }
            ReflectSet reflectSet = null;

            returnDelegate = delegate(object obj, object value) { reflectSet(obj, value); };
            cacheSet.Add(cacheKey, returnDelegate);

            FieldInfo fieldInfo =
                classType.GetField(fieldName, EasyIL.ALLATTR);            //获得属性对象FieldInfo

            reflectSet = delegate(object obj, object value)
            {
                fieldInfo.SetValue(obj, value);
            };

            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                Type argType = typeof(object);
                EasyIL il    = new EasyIL(null, classType.Module, argType, argType);

                ILField objField = il.CreateField(fieldInfo, il["0"]);//根据对象和属性对象 创建IL参数
                objField.SetValue(il["1"].StValue());
                il.Return();
                reflectSet = (ReflectSet)il.CreateDelegate(typeof(ReflectSet));
            });


            return(returnDelegate);
        }
Exemplo n.º 2
0
        //获取一个获取特定字段的值的委托
        /// <summary>
        /// 获取一个获取特定字段的值的委托
        /// <para>返回结果:
        /// </para>一个object ReflectGet(object obj)类型的委托
        /// </summary>
        /// <param name="classType">需要获取字段的类</param>
        /// <param name="fieldName">字段名称</param>
        public static ReflectGet GetFieldValue(Type classType, string fieldName)
        {
            ReflectGet returnDelegate;

            string cacheKey = classType.FullName + SPACE + fieldName;

            if (cacheGet.TryGetValue(cacheKey, out returnDelegate))
            {
                return(returnDelegate);
            }
            cacheGet.Add(cacheKey, returnDelegate);

            FieldInfo fieldInfo = classType.GetField(fieldName, EasyIL.ALLATTR);

            ReflectGet reflectGet = delegate(object obj)
            {
                return(fieldInfo.GetValue(obj));
            };

            ThreadPool.QueueUserWorkItem(delegate(object o)
            {
                Type returnType = typeof(object);
                Type argType    = typeof(object);

                EasyIL il = new EasyIL(returnType, classType.Module, argType);

                ILField objfield = il.CreateField(fieldInfo, il["0"]);

                il.Return(objfield);

                reflectGet = (ReflectGet)il.CreateDelegate(typeof(ReflectGet));
            });

            returnDelegate = delegate(object obj) { return(reflectGet(obj)); };
            return(returnDelegate);
        }