zoukankan      html  css  js  c++  java
  • 泛型反射性能优化

    一,泛型反射优化:基本思路,根据泛型缓存原理(静态构造+静态字段)

       public class Accessor<S>
        {
            /// <summary>
            /// 属性类型
            /// </summary>
            public static PropertyInfo[] PropertyTypes { get; private set; }
            /// <summary>
            /// 实体类型
            /// </summary>
            public static Type type { get; private set; }
    
            static Accessor()
            {
                type = typeof(S);
                PropertyTypes = type.GetProperties();
            }
        }

    二,调用逻辑

     Stopwatch st = new Stopwatch();
                st.Start();
    
                for (int i = 0; i < 10000000; i++)
                {
                    var ss = Accessor<Person>.PropertyTypes;
                }
                st.Stop();
                var str1 = st.ElapsedMilliseconds.ToString();
    
    
                st.Reset();
                st.Start();
                for (int i = 0; i < 10000000; i++)
                {
                    Type type = typeof(Person);
                    var ss = type.GetProperties();
                }
                st.Stop();
                var str2 = st.ElapsedMilliseconds.ToString();
  • 相关阅读:
    1023. 组个最小数
    1021. 个位数统计
    *1020. 月饼
    *1019. 数字黑洞
    1016. 部分A+B
    *1014. 福尔摩斯的约会
    *1013. 数素数
    *1012. 数字分类
    1011. A+B和C
    *1008. 数组元素循环右移问题
  • 原文地址:https://www.cnblogs.com/May-day/p/11591802.html
Copyright © 2011-2022 走看看