Caching 扩展企业库缓存块-如何获取MyCacheManager实例?
由于CacheManager的默认实现不提供GetItemsOfType(以及其他许多),因此我考虑构建自己的:Caching 扩展企业库缓存块-如何获取MyCacheManager实例?,caching,enterprise-library,extending,Caching,Enterprise Library,Extending,由于CacheManager的默认实现不提供GetItemsOfType(以及其他许多),因此我考虑构建自己的: [ConfigurationElementType(typeof(CustomCacheManagerData))] public class MyCacheManager : ICacheManager { //The same constructor as in CacheAppBlock - CacheManager, but it's public here:
[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager
{
//The same constructor as in CacheAppBlock - CacheManager, but it's public here:
public MyCacheManager(Cache realCache, BackgroundScheduler scheduler, ExpirationPollTimer pollTimer)
{
this.realCache = realCache;
this.scheduler = scheduler;
this.pollTimer = pollTimer;
}
//the other code is basically copy/paste from CacheManager in EntLib, with some of my methods like:
public T[] GetItemsOfType<T>()
{
return realCache.CurrentCacheState.Values.OfType<T>().ToArray();
}
//I also have some other custom code on the underlying Hashtable in realCache
}
抛出异常,表示MyCacheManager中没有构造函数(但有,与EntLib的CacheManager中相同,只是它们在我的类中是公共的…这是因为
MyCacheManager
与EntLib不完全一样!我不是指那些额外的方法。看看这些声明
原始缓存管理器:
[ConfigurationElementType(typeof(CacheManagerData))]
public class CacheManager : IDisposable, ICacheManager
[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager
MyCacheManager:
[ConfigurationElementType(typeof(CacheManagerData))]
public class CacheManager : IDisposable, ICacheManager
[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager
除了名称差异(并且您没有扩展IDisposable),请注意元素类型属性
你正在使用(你必须)定制的。自定义的构造函数需要一个以NameValueCollection作为参数的构造函数
public MyCacheManager(NameValueCollection collection)
可以说,它是一个通用的配置驱动程序,因此不能指望它知道如何使用由缓存对象、调度程序和轮询计时器组成的3参数构造函数来创建实例。相反,它通过需要手动解析的基本NameValueCollection传入这些值(或配置文件中设置为属性的任何内容)
另请参见: