//获取一个设置特定属性的值的委托 /// <summary> /// 获取一个设置特定属性的值的委托 /// <para>返回结果: /// </para>一个void ReflectSet(object obj, object value)类型的委托 /// </summary> /// <param name="classType">需要设置属性的类</param> /// <param name="propertyName">属性名称</param> public static ReflectSet SetPropertyValue(Type classType, string propertyName) { ReflectSet returnDelegate; string cacheKey = classType.FullName + SPACE + propertyName; if (cacheSet.TryGetValue(cacheKey, out returnDelegate)) { return(returnDelegate); } ReflectSet reflectSet = null; returnDelegate = delegate(object obj, object value) { reflectSet(obj, value); }; cacheSet.Add(cacheKey, returnDelegate); PropertyInfo propertyInfo = classType.GetProperty(propertyName, EasyIL.ALLATTR); //获得属性对象PropertyInfo reflectSet = delegate(object obj, object value) { propertyInfo.GetSetMethod().Invoke(obj, new object[] { value }); }; ThreadPool.QueueUserWorkItem(delegate(object o) { Type argType = typeof(object); EasyIL il = new EasyIL(null, classType.Module, argType, argType); ILProperty objProperty = il.CreateProperty(propertyInfo, il["0"]); //根据对象和属性对象 创建IL参数 objProperty.SetValue(il["1"].StValue()); il.Return(); reflectSet = (ReflectSet)il.CreateDelegate(typeof(ReflectSet)); }); return(returnDelegate); }
//获取一个设置特定字段的值的委托 /// <summary> /// 获取一个设置特定字段的值的委托 /// <para>返回结果: /// </para>一个void ReflectSet(object obj, object value)类型的委托 /// </summary> /// <param name="classType">需要设置字段的类</param> /// <param name="fieldName">字段名称</param> public static ReflectSet SetFieldValue(Type classType, string fieldName) { ReflectSet returnDelegate; string cacheKey = classType.FullName + SPACE + fieldName; if (cacheSet.TryGetValue(cacheKey, out returnDelegate)) { return(returnDelegate); } ReflectSet reflectSet = null; returnDelegate = delegate(object obj, object value) { reflectSet(obj, value); }; cacheSet.Add(cacheKey, returnDelegate); FieldInfo fieldInfo = classType.GetField(fieldName, EasyIL.ALLATTR); //获得属性对象FieldInfo reflectSet = delegate(object obj, object value) { fieldInfo.SetValue(obj, value); }; ThreadPool.QueueUserWorkItem(delegate(object o) { Type argType = typeof(object); EasyIL il = new EasyIL(null, classType.Module, argType, argType); ILField objField = il.CreateField(fieldInfo, il["0"]);//根据对象和属性对象 创建IL参数 objField.SetValue(il["1"].StValue()); il.Return(); reflectSet = (ReflectSet)il.CreateDelegate(typeof(ReflectSet)); }); return(returnDelegate); }