【C#】使用DynamicMethod代替PropertyInfo.Get/SetValue提高性能

【C#】使用DynamicMethod代替PropertyInfo.Get/SetValue提高性能

上代码

DynamicMethodHelper

namespace System.Reflection.Emit
{
    public static class DynamicMethodHelper
    {
        private static readonly Dictionary<string, Action<object, object>> _dynamicMethods = new Dictionary<string, Action<object, object>>();
        private static readonly Dictionary<string, Func<object, object>> _dynamicGetMethods = new Dictionary<string, Func<object, object>>();

        private static readonly object _lockObj = new object();
        private static readonly object _lockSetObj = new object();

        private static string ResolveSetName(PropertyInfo propertyInfo, Type type) => $"{type.FullName}_set_{propertyInfo.Name}";

        public static Action<object, object> ResolveSetValueMethod<T>(PropertyInfo property) => ResolveSetValueMethod(property, typeof(T));

        public static Action<object, object> ResolveSetValueMethod(PropertyInfo property, Type type)
        {
            string name = ResolveSetName(property, type);
            if (_dynamicMethods.ContainsKey(name)) { return _dynamicMethods[name]; }
            lock (_lockObj)
            {
                var method = new DynamicMethod(
                name,
                null,
                new[] { typeof(object), typeof(object) },
                type.Module);

                var ilGenerator = method.GetILGenerator();

                ilGenerator.Emit(OpCodes.Ldarg_0); // 加载第一个参数:object target
                ilGenerator.Emit(OpCodes.Castclass, property.DeclaringType); // 转换为目标类型
                ilGenerator.Emit(OpCodes.Ldarg_1); // 加载第二个参数:object value

                if (property.PropertyType.IsValueType)
                {
                    ilGenerator.Emit(OpCodes.Unbox_Any, property.PropertyType); // 如果是值类型,进行拆箱
                }
                else
                {
                    ilGenerator.Emit(OpCodes.Castclass, property.PropertyType); // 如果是引用类型,进行类型转换
                }

                ilGenerator.Emit(OpCodes.Callvirt, property.GetSetMethod()); // 调用属性的set方法
                ilGenerator.Emit(OpCodes.Ret); // 返回

                if (_dynamicMethods.ContainsKey(name)) { return _dynamicMethods[name]; }

                _dynamicMethods.Add(name, (Action<object, object>)method.CreateDelegate(typeof(Action<object, object>)));
            }
            return _dynamicMethods[name];
        }

        private static string ResolveGetName(PropertyInfo propertyInfo, Type type) => $"{type.FullName}_get_{propertyInfo.Name}";

        public static Func<object, object> ResolveGetValueMethod<T>(PropertyInfo property) => ResolveGetValueMethod(property, typeof(T));

        public static Func<object, object> ResolveGetValueMethod(PropertyInfo property, Type type)
        {
            string name = ResolveGetName(property, type);
            if (_dynamicGetMethods.ContainsKey(name)) { return _dynamicGetMethods[name]; }
            lock (_lockSetObj)
            {
                var method = new DynamicMethod(
                $"{type.FullName}_get_{name}",
                typeof(object),
                new[] { typeof(object) },
                type.Module);

                var ilGenerator = method.GetILGenerator();

                ilGenerator.Emit(OpCodes.Ldarg_0); // 加载第一个参数:object target
                ilGenerator.Emit(OpCodes.Castclass, property.DeclaringType); // 转换为目标类型
                ilGenerator.Emit(OpCodes.Callvirt, property.GetGetMethod()); // 调用属性的get方法

                if (property.PropertyType.IsValueType)
                {
                    ilGenerator.Emit(OpCodes.Box, property.PropertyType); // 如果是值类型,进行装箱
                }

                ilGenerator.Emit(OpCodes.Ret); // 返回

                if (_dynamicGetMethods.ContainsKey(name)) { return _dynamicGetMethods[name]; }

                _dynamicGetMethods.Add(name, (Func<object, object>)method.CreateDelegate(typeof(Func<object, object>)));
            }
            return _dynamicGetMethods[name];
        }
    }
}

扩展方法方便调用

using System.Reflection.Emit;

namespace System.Reflection
{
    public static class PropertyInfoExtensions
    {
        public static object EmitGetValue(this PropertyInfo property, object obj)
        {
            var getMethod = DynamicMethodHelper.ResolveGetValueMethod(property, obj.GetType());
            return getMethod?.Invoke(obj);
        }
        
        public static void EmitSetValue(this PropertyInfo property, object obj, object value)
        {
            var setMethod = DynamicMethodHelper.ResolveSetValueMethod(property, obj.GetType());
            setMethod.Invoke(obj, value);
        }
    }
}

调用

	var obj = new Person(){
		Name = "Theo"
	};
	var property = typeof(Person).GetProperty("Name");
	Console.WriteLine($"EmiGetValue:{property.EmiGetValue(obj)}");
	property.EmiSetValue(obj, "Thea");
	Console.WriteLine($"obj.Name:{obj.Name}");

结语

显然,性能最好的方式是直接显示调用。在无法显示调用属性的时候,DynamicMethod提供了一种性能优于PropertyInfo.Get/SetValue的方案。使用DynamicMethod构造getter/setter是比较耗时的,因此使用了缓存。

此方案仅为笔者灵感一现脑补出来的。另外通过构造Expression的方式也可以实现对属性的读写操作,可能会在下篇blog中探讨。
提示:代码未经过生产环境验证,请自行验证后使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值