90 lines
1.8 KiB
C#
90 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace NBF
|
|
{
|
|
public interface IConfigContext
|
|
{
|
|
// 定义非泛型接口
|
|
}
|
|
|
|
public class ConfigContext<T> : IConfigContext where T : ConfigBase
|
|
{
|
|
private static List<T> _cacheList = new List<T>();
|
|
|
|
#region Cache
|
|
|
|
public void Association(List<T> list)
|
|
{
|
|
if (list != null)
|
|
{
|
|
_cacheList = list;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
public int Count()
|
|
{
|
|
return _cacheList.Count;
|
|
}
|
|
|
|
public int Count(Func<T, bool> predicate)
|
|
{
|
|
return _cacheList.Count(predicate);
|
|
}
|
|
|
|
public T Get(int key)
|
|
{
|
|
return First(key);
|
|
}
|
|
|
|
public T Fist()
|
|
{
|
|
return _cacheList.First();
|
|
}
|
|
|
|
public T Last()
|
|
{
|
|
return _cacheList.Last();
|
|
}
|
|
|
|
public T Fist(Predicate<T> match)
|
|
{
|
|
return Get(match);
|
|
}
|
|
|
|
public T Last(Predicate<T> match)
|
|
{
|
|
return _cacheList.FindLast(match);
|
|
}
|
|
|
|
public T Get(Predicate<T> match)
|
|
{
|
|
return _cacheList.Find(match);
|
|
}
|
|
|
|
public T GetRandom()
|
|
{
|
|
Random random = new Random();
|
|
// 随机从列表中取一个对象
|
|
return _cacheList[random.Next(_cacheList.Count)];
|
|
}
|
|
|
|
public List<T> GetList()
|
|
{
|
|
return _cacheList;
|
|
}
|
|
|
|
public List<T> GetList(Predicate<T> match)
|
|
{
|
|
return _cacheList.FindAll(match);
|
|
}
|
|
|
|
private T First(int key)
|
|
{
|
|
return _cacheList.Find(t => t.id == key);
|
|
}
|
|
}
|
|
} |