public DTOProperty(PropertyInfo info, FastPropertySetHandler handler) : this() { Info = info; Handler = handler; Name = info.Name; }
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); } }
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); }
/// <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); } }
public void GetPropertySetterTest() { PropertyInfo propInfo = null; // TODO: 初始化为适当的值 FastPropertySetHandler expected = null; // TODO: 初始化为适当的值 FastPropertySetHandler actual; actual = DynamicCalls.GetPropertySetter(propInfo); Assert.AreEqual(expected, actual); Assert.Inconclusive("验证此测试方法的正确性。"); }
public PropertyHandler(PropertyInfo property) { if (property.CanWrite) { this.mSetValue = DynamicCalls.GetPropertySetter(property); } if (property.CanRead) { this.mGetValue = DynamicCalls.GetPropertyGetter(property); } this.mProperty = property; }
public PropertyHandler(PropertyInfo property) { if (property.CanWrite) { this.mSetValue = DynamicCalls.GetPropertySetter(property); } if (property.CanRead) { this.mGetValue = DynamicCalls.GetPropertyGetter(property); } this.mProperty = property; }
/// <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; } }
public DTOProperty(PropertyInfo info, FastPropertySetHandler handler, FastPropertyGetHandler getHandler) : this(info, handler) { GetHandler = getHandler; }