属性值动态获取和赋值(反射、表达式、Emit)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Reflection.Emit;

using System.Linq.Expressions;


namespace Util
{
    /// <summary>
    /// 属性值动态获取和赋值(get、set)
    /// </summary>
    public class PropertyUtil
    {
        /// <summary>
        /// 反射获取对象的属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object ReflectGetter(object obj, string propertyName)
        {
            var type = obj.GetType();
            var propertyInfo = type.GetProperty(propertyName);
            var propertyValue = propertyInfo.GetValue(obj);
            return propertyValue;
        }


        /// <summary>
        /// 反射设置对象的属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        public static void ReflectSetter(object obj, string propertyName, object propertyValue)
        {
            var type = obj.GetType();
            var propertyInfo = type.GetProperty(propertyName);
            propertyInfo.SetValue(obj, propertyValue);
        }


        /// <summary>
        /// 表达式获取对象的属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static Func<T, object> ExpresionGetter<T>(string propertyName)
        {
            var type = typeof(T);
            var property = type.GetProperty(propertyName);


            对象实例
            var parameterExpression = Expression.Parameter(typeof(object), "obj");


            转换参数为真实类型
            var unaryExpression = Expression.Convert(parameterExpression, type);


            调用获取属性的方法
            var callMethod = Expression.Call(unaryExpression, property.GetGetMethod());
            var expression = Expression.Lambda<Func<T, object>>(callMethod, parameterExpression);


            return expression.Compile();
        }


        /// <summary>
        /// 表达式设置对象的属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static Action<T, object> ExpresionSetter<T>(string propertyName)
        {
            var type = typeof(T);
            var property = type.GetProperty(propertyName);


            var objectParameterExpression = Expression.Parameter(typeof(object), "obj");
            var objectUnaryExpression = Expression.Convert(objectParameterExpression, type);


            var valueParameterExpression = Expression.Parameter(typeof(object), "val");
            var valueUnaryExpression = Expression.Convert(valueParameterExpression, property.PropertyType);


            调用给属性赋值的方法
            var body = Expression.Call(objectUnaryExpression, property.GetSetMethod(), valueUnaryExpression);
            var expression = Expression.Lambda<Action<T, object>>(body, objectParameterExpression, valueParameterExpression);


            return expression.Compile();
        }


        /// <summary>
        /// Emit获取对象的属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static Func<T, object> EmitGetter<T>(string propertyName)
        {
            var type = typeof(T);


            var dynamicMethod = new DynamicMethod("get_" + propertyName, typeof(object), new[] { type }, type);
            var iLGenerator = dynamicMethod.GetILGenerator();
            iLGenerator.Emit(OpCodes.Ldarg_0);


            var property = type.GetProperty(propertyName);
            iLGenerator.Emit(OpCodes.Callvirt, property.GetMethod);


            if (property.PropertyType.IsValueType)
            {
                // 如果是值类型,装箱
                iLGenerator.Emit(OpCodes.Box, property.PropertyType);
            }
            else
            {
                // 如果是引用类型,转换
                iLGenerator.Emit(OpCodes.Castclass, property.PropertyType);
            }


            iLGenerator.Emit(OpCodes.Ret);


            return dynamicMethod.CreateDelegate(typeof(Func<T, object>)) as Func<T, object>;
        }


        /// <summary>
        /// Emit设置对象的属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static Action<T, object> EmitSetter<T>(string propertyName)
        {
            var type = typeof(T);


            var dynamicMethod = new DynamicMethod("EmitCallable", null, new[] { type, typeof(object) }, type.Module);
            var iLGenerator = dynamicMethod.GetILGenerator();


            var callMethod = type.GetMethod("set_" + propertyName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
            var parameterInfo = callMethod.GetParameters()[0];
            var local = iLGenerator.DeclareLocal(parameterInfo.ParameterType, true);


            iLGenerator.Emit(OpCodes.Ldarg_1);
            if (parameterInfo.ParameterType.IsValueType)
            {
                // 如果是值类型,拆箱
                iLGenerator.Emit(OpCodes.Unbox_Any, parameterInfo.ParameterType);
            }
            else
            {
                // 如果是引用类型,转换
                iLGenerator.Emit(OpCodes.Castclass, parameterInfo.ParameterType);
            }


            iLGenerator.Emit(OpCodes.Stloc, local);
            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldloc, local);


            iLGenerator.EmitCall(OpCodes.Callvirt, callMethod, null);
            iLGenerator.Emit(OpCodes.Ret);


            return dynamicMethod.CreateDelegate(typeof(Action<T, object>)) as Action<T, object>;
        }
    }
}


单元测试

using Util;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Tests.Util
{
    [TestClass]
    public class PropertyUtil_Test
    {
        /// <summary>
        /// 
        /// </summary>
        public class Logic
        {
            /// <summary>
            /// 
            /// </summary>
            public string Name { get; set; }


        }


        [TestMethod]
        public void Reflect_Test()
        {
            var model = new Logic();


            // 设置 model 的 Name 字段值 = 测试
            PropertyUtil.ReflectSetter(model, "Name", "测试");


            // 获取 model 的 Name 字段值
            var value = PropertyUtil.ReflectGetter(model, "Name");


            Assert.IsTrue(value.ToString() == "测试");
        }


        [TestMethod]
        public void Expresion_Test()
        {
            var model = new Logic();


            // 设置 model 的 Name 字段值 = 测试
            var setterMethod = PropertyUtil.ExpresionSetter<Logic>("Name");
            setterMethod(model, "测试");


            // 获取 model 的 Name 字段值
            var getterMethod = PropertyUtil.ExpresionGetter<Logic>("Name");
            var value = getterMethod(model);


            Assert.IsTrue(value.ToString() == "测试");
        }


        [TestMethod]
        public void Emit_Test()
        {
            var model = new Logic();


            // 设置 model 的 Name 字段值 = 测试
            var setterMethod = PropertyUtil.EmitSetter<Logic>("Name");
            setterMethod(model, "测试");


            // 获取 model 的 Name 字段值
            var getterMethod = PropertyUtil.EmitGetter<Logic>("Name");
            var value = getterMethod(model);


            Assert.IsTrue(value.ToString() == "测试");
        }
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在父组件中获取子组件的,可以通过以下两种方式实现: 1. 通过 props 传递 在子组件中定义一个 props,然后将子组件中的作为 props 的传递给父组件。父组件就可以通过 props 获取子组件的。 例如,子组件中定义一个名为 `childValue` 的 props,然后将组件中的作为 `childValue` 的传递给父组件。 ```vue <template> <div> <input v-model="value" /> </div> </template> <script> export default { props: { childValue: { type: String, default: '' } }, data() { return { value: this.childValue } }, watch: { value(newValue) { this.$emit('update:childValue', newValue) } } } </script> ``` 在父组件中使用子组件时,将子组件的 `childValue` 绑定到一个父组件的属性中。 ```vue <template> <div> <child-component :childValue.sync="parentValue"></child-component> <p>子组件的是:{{ parentValue }}</p> </div> </template> <script> export default { data() { return { parentValue: '' } } } </script> ``` 这里使用了 `.sync` 修饰符来实现双向绑定。 2. 通过 $refs 获取 在子组件中定义一个 ref,在父组件中通过 $refs 获取子组件,然后就可以直接访问子组件的属性和方法。 例如,在子组件中定义一个名为 `childInput` 的 ref。 ```vue <template> <div> <input ref="childInput" v-model="value" /> </div> </template> <script> export default { data() { return { value: '' } } } </script> ``` 在父组件中,可以通过 `$refs` 获取子组件,然后访问子组件的属性和方法。 ```vue <template> <div> <child-component ref="child"></child-component> <p>子组件的是:{{ $refs.child.value }}</p> </div> </template> <script> export default { mounted() { console.log(this.$refs.child.value) // undefined,因为子组件还没有渲染完成 this.$nextTick(() => { console.log(this.$refs.child.value) // 可以获取子组件的 }) } } </script> ``` 需要注意的是,在父组件中通过 `$refs` 获取子组件时,需要在子组件渲染完成后才能访问子组件的属性和方法。因此,需要在 `$nextTick` 回调函数中访问子组件的属性和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值