private List <string> GetCacheKeys() { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; Type cacheType = cache.GetType(); MemoryCache memoryCache; if (cacheType == typeof(RabbitMQSynchronizedCache)) { memoryCache = (MemoryCache)cacheType.GetField("memoryCache", flags).GetValue(cache); } else if (cacheType == typeof(MemoryCache)) { memoryCache = (MemoryCache)cache; } else { throw new ArgumentException("不支持的缓存类型"); } var field = memoryCache.GetType().GetField("memoryCache", BindingFlags.NonPublic | BindingFlags.Static); Microsoft.Extensions.Caching.Memory.MemoryCache subCache = (Microsoft.Extensions.Caching.Memory.MemoryCache)field.GetValue(memoryCache); var entries = subCache.GetType().GetField("_entries", flags).GetValue(subCache); var cacheItems = entries as IDictionary; List <string> keys = new List <string>(); if (cacheItems != null) { foreach (DictionaryEntry cacheItem in cacheItems) { string key = cacheItem.Key.ToString(); keys.Add(key); } } return(keys); }
public List <string> GetCacheKeys() { const System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic; var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache); var cacheItems = entries as System.Collections.IDictionary; var keys = new List <string>(); if (cacheItems == null) { return(keys); } foreach (System.Collections.DictionaryEntry cacheItem in cacheItems) { keys.Add(cacheItem.Key.ToString()); } return(keys); }