/// <summary>
/// 唯讀緩衝輔助
/// </summary>
/// <typeparam name="KeyType">鍵類型</typeparam>
/// <typeparam name="ValueType">實值型別</typeparam>
public static class Cache<KeyType, ValueType>
{
public delegate ValueType GetValue(KeyType key);
private static Dictionary<KeyType, ValueType> data=new Dictionary<KeyType,ValueType>();
/// <summary>
/// 擷取緩衝中的資料
/// </summary>
/// <param name="key">鍵</param>
/// <param name="get">Lambda運算式,當緩衝不存在時擷取值的委託</param>
/// <returns></returns>
public static ValueType Get(KeyType key, GetValue get)
{
if (data.ContainsKey(key))
return data[key];
else
{
ValueType value = get(key);
data.Add(key,value);
return value;
}
}
}
/// <summary>
/// 唯讀緩衝輔助
/// </summary>
/// <typeparam name="KeyType">鍵類型</typeparam>
/// <typeparam name="ValueType">實值型別</typeparam>
public static class Cache<KeyType, ValueType>
{
public delegate ValueType GetValue(KeyType key);
private static Dictionary<KeyType, ValueType> data=new Dictionary<KeyType,ValueType>();
/// <summary>
/// 擷取緩衝中的資料
/// </summary>
/// <param name="key">鍵</param>
/// <param name="get">Lambda運算式,當緩衝不存在時擷取值的委託</param>
/// <returns></returns>
public static ValueType Get(KeyType key, GetValue get)
{
if (data.ContainsKey(key))
return data[key];
else
{
ValueType value = get(key);
data.Add(key,value);
return value;
}
}
}
使用例子:
反射MyModel對象所有屬性並且緩衝
PropertyInfo[] MyProperty = Cache<Type, PropertyInfo[]>.Get(typeof(MyModel), p => p.GetProperties());
public static class CacheHelpr<KeyType, ValueType>
{
public delegate ValueType GetValue(KeyType key);
private static Dictionary<KeyType, ValueType> data=new Dictionary<KeyType,ValueType>();
public static ValueType Get(KeyType key, GetValue get)
{
if (data.ContainsKey(key))
return data[key];
else
{
ValueType value = get(key);
data.Add(key,value);
return value;
}
}
}