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

推荐订阅源

J
Java Code Geeks
量子位
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
A
About on SuperTechFans
腾讯CDC
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
Last Week in AI
Last Week in AI
H
Help Net Security
WordPress大学
WordPress大学
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
博客园 - 【当耐特】
S
SegmentFault 最新的问题
美团技术团队
M
MIT News - Artificial intelligence
L
LangChain 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