Пример #1
0
        public static FastFieldSetter GetFieldSetter(FieldInfo fieldInfo)
        {
            FastFieldSetter res;

            if (setterCache.TryGetValue(fieldInfo, out res))
            {
                return(res);
            }

            lock (setterCache)
            {
                DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(void), new Type[] { typeof(object), typeof(object) }, fieldInfo.DeclaringType, true);
                ILGenerator   il            = dynamicMethod.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                if (fieldInfo.FieldType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
                }
                il.Emit(OpCodes.Stfld, fieldInfo);
                il.Emit(OpCodes.Ret);
                FastFieldSetter invoker = (FastFieldSetter)dynamicMethod.CreateDelegate(typeof(FastFieldSetter));
                setterCache[fieldInfo] = invoker;
                return(invoker);
            }
        }
        public virtual void SetPropertyValue(object obj, Type type, string propertyName, object value)
        {
#if NET2
            if (value == null)
            {
                SetPropertyValueReflection(obj, propertyName, value);
            }
            else
            {
                string          key    = obj.GetType().Name + "." + propertyName;
                FastFieldSetter setter = (FastFieldSetter)fastFieldSetterLookup[key];
                if (setter == null)
                {
                    FieldInfo fieldInfo = GetFieldInfo(obj, propertyName);
                    setter = FastFieldAccess.GetFieldSetter(fieldInfo);
                    fastFieldSetterLookup[key] = setter;
                }

                setter(obj, value);
            }
#else
            SetPropertyValueReflection(obj, propertyName, value);
#endif
        }