Ejemplo n.º 1
0
 public DTOProperty(PropertyInfo info, FastPropertySetHandler handler)
     : this()
 {
     Info    = info;
     Handler = handler;
     Name    = info.Name;
 }
Ejemplo n.º 2
0
        public static FastPropertySetHandler GetPropertySetter(PropertyInfo propInfo)
        {
            lock (dictSetter)
            {
                if (dictSetter.ContainsKey(propInfo))
                {
                    return((FastPropertySetHandler)dictSetter[propInfo]);
                }

                DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, null, new Type[] { typeof(object), typeof(object) }, propInfo.DeclaringType.Module);

                ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

                ilGenerator.Emit(OpCodes.Ldarg_0);

                ilGenerator.Emit(OpCodes.Ldarg_1);

                EmitCastToReference(ilGenerator, propInfo.PropertyType);

                ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetSetMethod(), null);

                ilGenerator.Emit(OpCodes.Ret);

                FastPropertySetHandler setter = (FastPropertySetHandler)dynamicMethod.CreateDelegate(typeof(FastPropertySetHandler));

                dictSetter.Add(propInfo, setter);

                return(setter);
            }
        }
Ejemplo n.º 3
0
        public static FastPropertySetHandler GetPropertySetter(PropertyInfo propInfo)
        {
            // generates a dynamic method to generate a FastPropertySetHandler delegate
            DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, null, new Type[] { typeof(object), typeof(object) }, propInfo.DeclaringType.Module);

            ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

            // loads the object into the stack
            ilGenerator.Emit(OpCodes.Ldarg_0);

            // loads the parameter from the stack
            ilGenerator.Emit(OpCodes.Ldarg_1);

            // cast to the proper type (unboxing if needed)
            EmitCastToReference(ilGenerator, propInfo.PropertyType);

            // calls the setter
            ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetSetMethod(), null);

            // terminates the call
            ilGenerator.Emit(OpCodes.Ret);

            // converts the DynamicMethod to a FastPropertyGetHandler delegate to get the property
            FastPropertySetHandler setter = (FastPropertySetHandler)dynamicMethod.CreateDelegate(typeof(FastPropertySetHandler));

            return(setter);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 设置属性值
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="property"></param>
 /// <param name="value"></param>
 public static void SetPropertyValue(object obj, PropertyInfo property, object value)
 {
     if (property.CanWrite)
     {
         FastPropertySetHandler propertySetter = DynamicCalls.GetPropertySetter(property);
         value = ConvertValue(property.PropertyType, value);
         propertySetter(obj, value);
     }
 }
Ejemplo n.º 5
0
        public void GetPropertySetterTest()
        {
            PropertyInfo           propInfo = null; // TODO: 初始化为适当的值
            FastPropertySetHandler expected = null; // TODO: 初始化为适当的值
            FastPropertySetHandler actual;

            actual = DynamicCalls.GetPropertySetter(propInfo);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 6
0
 public PropertyHandler(PropertyInfo property)
 {
     if (property.CanWrite)
     {
         this.mSetValue = DynamicCalls.GetPropertySetter(property);
     }
     if (property.CanRead)
     {
         this.mGetValue = DynamicCalls.GetPropertyGetter(property);
     }
     this.mProperty = property;
 }
Ejemplo n.º 7
0
 public PropertyHandler(PropertyInfo property)
 {
     if (property.CanWrite)
     {
         this.mSetValue = DynamicCalls.GetPropertySetter(property);
     }
     if (property.CanRead)
     {
         this.mGetValue = DynamicCalls.GetPropertyGetter(property);
     }
     this.mProperty = property;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 快速设置属性值
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="property"></param>
 /// <param name="value"></param>
 public static void SetPropertyValue(object obj, PropertyInfo property, object value)
 {
     if (!property.CanWrite)
     {
         return;
     }
     try
     {
         FastPropertySetHandler setter = DynamicCalls.GetPropertySetter(property);
         value = ConvertValue(property.PropertyType, value);
         setter(obj, value);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 9
0
 public DTOProperty(PropertyInfo info, FastPropertySetHandler handler, FastPropertyGetHandler getHandler)
     : this(info, handler)
 {
     GetHandler = getHandler;
 }