快速动态访问属性

    public static class EmitDynamicProperty
    {
        private static ConcurrentDictionary<string, Delegate> action = new ConcurrentDictionary<string, Delegate>();
        public static TRet DynamicGetProperty<TRet>(this object obj, string propertyName)
        {
            var type = obj.GetType();
            string key = string.Concat(type.FullName, "_", propertyName);
            var gater = action.GetOrAdd(key, k =>
            {
                var method = type.GetProperty(propertyName).GetGetMethod();

                var dynamicMethod = new DynamicMethod(string.Empty, typeof(TRet), new Type[] { typeof(object) });

                var ilGen = dynamicMethod.GetILGenerator();
                ilGen.Emit(OpCodes.Ldarg_0);
                ilGen.Emit(OpCodes.Castclass, type);
                ilGen.Emit(OpCodes.Callvirt, method);
                if (method.ReturnType.IsValueType && (!typeof(TRet).IsValueType))
                {
                    ilGen.Emit(OpCodes.Box, method.ReturnType);
                }
                ilGen.Emit(OpCodes.Ret);

                return dynamicMethod.CreateDelegate(typeof(Func<object, TRet>));

            });

            return ((Func<object, TRet>)gater)(obj);

        }
    }

  

posted @ 2012-07-31 10:21  阿牛  阅读(284)  评论(0编辑  收藏  举报