zoukankan      html  css  js  c++  java
  • 泛型

    泛型方法:就是对方法重载的优化,方法重载可以有不同的返回类型或者参数。方法重载和默认参数能会很好的优化重载的写法。

    只是在最终运行的时候,还是展开了。就是说有几个泛型调用,最终还会展开成几个泛型方法。

    泛型是个代码模板(类,接口,委托,方法),留下了类型填充的“空位”

    这是泛型缓存的基础。

    泛型缓存要有静态构造函数,是泛型类。

    枚举的描述的泛型缓存就不好做。这是字典缓存

     public static class EnumDescription
        {
            private static Dictionary<Type, string> _genericCache = new Dictionary<Type, string>();
    
            public static string GetEnumDescription<T>(this T enumValue) where T : Enum
            {
                if (_genericCache.ContainsKey(typeof(T)))
                {
                    return _genericCache.GetValueOrDefault(typeof(T));
                }
    
                else
                {
                    string value = enumValue.ToString();
                    FieldInfo field = enumValue.GetType().GetField(value);
                    object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
                    if (objs == null || objs.Length == 0)    //当描述属性没有时,直接返回名称
                        return value;
                    value = ((DescriptionAttribute)objs[0]).Description;
                    _genericCache.Add(typeof(T), value);
                    return value;
                }
            }
        }

     另外,定义一个泛型类,它里面的静态方法(非构造函数)是没法访问的。

    气功波(18037675651)
  • 相关阅读:
    bloom filter
    【转】单实例
    Log Structured Merge Trees(LSM) 原理
    【转】南网成立始末
    变电站综合自动化系统
    bsp tree
    Private Bytes,Working Set,Virtual Size的区别
    kdtree
    asp.net下载文件几种方式
    C# FTP操作
  • 原文地址:https://www.cnblogs.com/qgbo/p/11347481.html
Copyright © 2011-2022 走看看