Beispiel #1
0
        private static long TestCompiled()
        {
            DataTestForPerf src = new DataTestForPerf();
            DataTestForPerf dst = new DataTestForPerf();

            PropertyInfo piSource = src.GetType().GetProperty("Prop1");
            PropertyInfo piDest   = dst.GetType().GetProperty("Prop1");

            GetHandlerDelegate <int> getSrc = GetSetUtils.CreateGetHandler <int>(piSource);
            SetHandlerDelegate <int> setDst = GetSetUtils.CreateSetHandler <int>(piDest);

            src.PropertyChanged += delegate
            {
                setDst(dst, getSrc(src));
            };

            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 1; i <= NBTURNS; i++)
            {
                src.Prop1 = i;
            }
            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }
Beispiel #2
0
        public void Bind(object source, PropertyInfo piSource, object destination,
                         PropertyInfo piDest,
                         IBinderConverter converter,
                         SynchronizationContext applyBindingContext)
        {
            weakSrc         = new WeakReference(source);
            weakDst         = new WeakReference(destination);
            _Converter      = converter;
            _PropNameSource = piSource.Name;
            _PropNameDest   = piDest.Name;
            gethandler      = GetSetUtils.CreateGetHandler <TValueSource>(piSource);

            //le set handler devrait être sur le type destination

            if (_Converter != null)
            {
                _CurrentChanged = OnValueChanged1;
                sethandlerDest  = GetSetUtils.CreateSetHandler <TValueDest>(piDest);
            }
            else
            {
                _CurrentChanged = OnValueChanged;
                sethandler      = GetSetUtils.CreateSetHandler <TValueSource>(piDest);
            }

            DataBinder.AddNotify <TValueSource>(source, piSource.Name, gethandler, _CurrentChanged, applyBindingContext);
        }
Beispiel #3
0
        /// <summary>
        /// créée une méthode dynamique pour acceder à une propriété sans la réflection
        /// </summary>
        /// <typeparam name="TValue">type de la valeur</typeparam>
        /// <typeparam name="TInstance">type contenant la propriété</typeparam>
        /// <param name="propertyInfo">propertyInfo de la propriété concernée</param>
        /// <returns></returns>
        public static SetHandlerDelegate <TValue> CreateSetHandler <TValue>(PropertyInfo propertyInfo)
        {
            MethodInfo setMethod = propertyInfo.GetSetMethod(true);

            if (_Dico.ContainsKey(setMethod))
            {
                return((SetHandlerDelegate <TValue>)_Dico[setMethod]);
            }
#if SILVERLIGHT
            //DynamicMethod dynamicSet = new DynamicMethod("DynamicSet" + propertyInfo.Name,
            //                                                typeof(void),
            //                                                new Type[] { typeof(object), typeof(TValue) });
            SetHandlerDelegate <TValue> Result = delegate(object sender, TValue value)
            {
                setMethod.Invoke(sender, new object[] { value });
            };
            //ILGenerator setGenerator = dynamicSet.GetILGenerator();

            //setGenerator.Emit(OpCodes.Ldarg_0);
            //setGenerator.Emit(OpCodes.Ldarg_1);
            //setGenerator.Emit(OpCodes.Call, setMethod);
            //setGenerator.Emit(OpCodes.Ret);
            //Type tDelegate = typeof(SetHandlerDelegate<TValue>);
            //SetHandlerDelegate<TValue> Result = (SetHandlerDelegate<TValue>)dynamicSet.CreateDelegate(tDelegate);
#else
            DynamicMethod dynamicSet = new DynamicMethod("DynamicSet" + propertyInfo.Name,
                                                         typeof(void),
                                                         new Type[] { typeof(object), typeof(TValue) },
                                                         propertyInfo.DeclaringType, true);
            ILGenerator setGenerator = dynamicSet.GetILGenerator();

            setGenerator.Emit(OpCodes.Ldarg_0);
            setGenerator.Emit(OpCodes.Ldarg_1);
            setGenerator.Emit(OpCodes.Call, setMethod);
            setGenerator.Emit(OpCodes.Ret);

            Type tDelegate = typeof(SetHandlerDelegate <TValue>);
            SetHandlerDelegate <TValue> Result = (SetHandlerDelegate <TValue>)dynamicSet.CreateDelegate(tDelegate);
#endif

            //mise en cache de la méthode
            _Dico[setMethod] = Result;
            return(Result);
        }