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;
}
}
}