泛型缓存&静态字典缓存&约束

using System;
using System.Collections.Generic;
using System.Text;

namespace Wchentest
{
    /// <summary>
    /// 字典缓存
    /// </summary>
    public class DictionaryCache
    {
        private static Dictionary<Type, string> _TypeTimeDictionary = null;
        static DictionaryCache()
        {
            Console.WriteLine("This is DictionaryCache  静态构造函数");
            _TypeTimeDictionary = new Dictionary<Type, string>();
        }
        public static string GetCache<T>()
        {
            Type type = typeof(Type);
            if (_TypeTimeDictionary.ContainsKey(type)!)
            {
                _TypeTimeDictionary[type] = string.Format("{0}_{1}",
                    typeof(T).FullName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
            }
            return _TypeTimeDictionary[type];
        }
    }
    /// <summary>
    /// 泛型缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericCache<T>
    {
        private static string _TypeTime = "";
        static GenericCache()
        {
            Console.WriteLine("This is GenericCache  静态构造函数");
            _TypeTime = string.Format("{0}_{1}", typeof(T).FullName,
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
        }
        public static string GetCache()
        {
            return _TypeTime;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Wchentest
{
    class InterFaceTest
    {
        //约束不能数密封的  父类或者接口有sealed关键字
        // 值类型约束
        public S GetT_0<S>()
            where S : struct
        {
            return default(S);
        }
        //引用类型约束
        public S GetT_1<S>()
            where S : class
        {
            return default(S);
        }
        //new() 无参数构造函数约束,S的构造函数必须是无参数的
        public S GetT_2<S>()
            where S : new()
        {
            return default(S);
        }
        //父类约束
        public S GetT_3<S>()
            where S : Man
        {
            return default(S);
        }
        //接口约束
        public S GetT_4<S>()
            where S : People
        {
            return default(S);
        }
    }
    public class Shannxi : Man, People
    {
        public string name { set; get; }
        public string address { set; get; }
        public int Id { set; get; }
        public string GetName()
        {
            return name;
        }
    }

    public class Chinese<T> where T : Man, People
    {
        public string name { set; get; }
        public string address { set; get; }
        public int Id { set; get; }
        public string GetName()
        {
            return name;
        }
    }


    public interface People
    {
        public string name { set; get; }
        public int Id { set; get; }
        public string GetName()
        {
            return name;
        }
    }

    public class Man
    {
        public string name { set; get; }
        public string address { set; get; }
        public int Id { set; get; }
        public string GetName()
        {
            return address;
        }
    }

    public class BirdClass
    {
        public string Name { set; get; }
    }
    #region  协变/逆变
    public class SparrowClass : BirdClass
    {
    }
    //协变
    public interface IWchen_out<out T>
    {
        T Get();

        //void Show(T t);
    }

    public class Wchen_out<T> : IWchen_out<T> where T: BirdClass
    {
        public T Get()
        {
            return default(T);
        }
        public void Show(T t)
        {
            t.Name = "out鸟";
        }
    }
    //逆变
    public interface IWchen_in<in T>
    {
    }

    public class Wchen_in<T> : IWchen_in<T>
    {
    }

    public interface IWchen_in_out<in T, out S>
    {
    }

    public class Wchen_in_out<T, S> : IWchen_in_out<T, S>
    {
    }
    #endregion

    struct XianPeople: People
    {
        public string name { set; get; }
        public int Id { set; get; }
        public string GetName()
        {
            return name;
        }
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值