惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
云风的 BLOG
云风的 BLOG
I
InfoQ
小众软件
小众软件
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
雷峰网
雷峰网
P
Proofpoint News Feed
腾讯CDC
H
Help Net Security
V
Visual Studio Blog
美团技术团队
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志

博客园 - Zhenway

Azure SignalR支持replication啦 Azure SignalR总览 定制json序列化 今天折腾这么一个正则 docfx组件介绍--YamlSerialization docfx daylybuild docfx开源啦 docfx组件介绍--MarkdownLite docfx预热中 合并批量请求 一个简单的Linq to TreeNode 在finally中调用一个需要await的方法 当泛型方法推断,扩展方法遇到泛型类型in/out时。。。 4.5你太黑了,不带这么玩TypeForwardedTo的 加载时预防并发执行 又发现一个msdn的坑 又发现个.net framework的坑 sql server死锁神器 踩到一个Emit的坑,留个纪念
一个非常简单的反射加速方案
Zhenway · 2014-02-24 · via 博客园 - Zhenway

不废话,直接上代码:

    class Program
    {
        private static readonly MethodInfo HelpMethod = typeof(Program).GetMethod("GetHelp", BindingFlags.NonPublic | BindingFlags.Static);

        static void Main(string[] args)
        {
            // you can cache the delegate, the type of delegate is always Func<object, object>
            var d = TestGet(typeof(Program).GetProperty("X"));
            var p = new Program { X = 1 };
            Console.WriteLine(d(p));
            Console.ReadLine();
        }

        private static Func<object, object> TestGet(PropertyInfo p)
        {
            var f = typeof(Func<,>).MakeGenericType(p.DeclaringType, p.PropertyType);
            var d = Delegate.CreateDelegate(f, p.GetGetMethod());
            return (Func<object, object>)Delegate.CreateDelegate(typeof(Func<object, object>), d, HelpMethod.MakeGenericMethod(p.DeclaringType, p.PropertyType));
        }

        private static object GetHelp<T, TResult>(Delegate d, object obj)
        {
            return ((Func<T, TResult>)d)((T)obj);
        }

        public int X { get; set; }
    }

View Code