public static FastPropertyGetHandler GetPropertyGetter(PropertyInfo propInfo) { lock (dictGetter) { if (dictGetter.ContainsKey(propInfo)) { return((FastPropertyGetHandler)dictGetter[propInfo]); } DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object) }, propInfo.DeclaringType.Module); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetGetMethod(), null); EmitBoxIfNeeded(ilGenerator, propInfo.PropertyType); ilGenerator.Emit(OpCodes.Ret); FastPropertyGetHandler getter = (FastPropertyGetHandler)dynamicMethod.CreateDelegate(typeof(FastPropertyGetHandler)); dictGetter.Add(propInfo, getter); return(getter); } }
public static FastPropertyGetHandler GetPropertyGetter(PropertyInfo propInfo) { lock (dictGetter) { if (dictGetter.ContainsKey(propInfo)) { return((FastPropertyGetHandler)dictGetter[propInfo]); } // generates a dynamic method to generate a FastPropertyGetHandler delegate DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object) }, propInfo.DeclaringType.Module); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); // loads the object into the stack ilGenerator.Emit(OpCodes.Ldarg_0); // calls the getter ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetGetMethod(), null); // creates code for handling the return value EmitBoxIfNeeded(ilGenerator, propInfo.PropertyType); // returns the value to the caller ilGenerator.Emit(OpCodes.Ret); // converts the DynamicMethod to a FastPropertyGetHandler delegate to get the property FastPropertyGetHandler getter = (FastPropertyGetHandler)dynamicMethod.CreateDelegate(typeof(FastPropertyGetHandler)); dictGetter.Add(propInfo, getter); return(getter); } }
/// <summary> /// 快速获取属性值 /// </summary> /// <param name="obj"></param> /// <param name="property"></param> /// <returns></returns> public static object GetPropertyValue(object obj, PropertyInfo property) { if (!property.CanRead) { return(null); } FastPropertyGetHandler getter = DynamicCalls.GetPropertyGetter(property); return(getter(obj)); }
public void GetPropertyGetterTest() { PropertyInfo propInfo = null; // TODO: 初始化为适当的值 FastPropertyGetHandler expected = null; // TODO: 初始化为适当的值 FastPropertyGetHandler actual; actual = DynamicCalls.GetPropertyGetter(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; }
/// <summary> /// 快速获取属性值 /// </summary> /// <param name="obj"></param> /// <param name="property"></param> /// <returns></returns> public static object GetPropertyValue(object obj, PropertyInfo property) { if (obj == null) { return(null); } if (!property.CanRead) { return(null); } try { FastPropertyGetHandler getter = DynamicCalls.GetPropertyGetter(property); return(getter(obj)); } catch (Exception ex) { throw ex; } }
public DTOProperty(PropertyInfo info, FastPropertySetHandler handler, FastPropertyGetHandler getHandler) : this(info, handler) { GetHandler = getHandler; }