這裏是羣友提供聽說是issue提的問題,高手回答的。
var coherentState = _cache.GetType().GetField("_coherentState", BindingFlags.NonPublic | BindingFlags.Instance); var coherentStateValue = coherentState.GetValue(_cache); var entriesCollection = coherentStateValue.GetType().GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance); var entriesCollectionValue = entriesCollection.GetValue(coherentStateValue) as ICollection; var keys = new List<string>(); if (entriesCollectionValue != null) { foreach (var item in entriesCollectionValue) { var methodInfo = item.GetType().GetProperty("Key"); var val = methodInfo.GetValue(item); keys.Add(val.ToString()); } } return keys;
由於.net 7源碼改變了,_entries外面又套了一層_coherentState,致使無法獲取所有鍵值。
通過查看源碼我自己修改瞭如下代碼:
var ggt = _cache.GetType(); var _coherentStates = ggt.GetRuntimeFields(); var keys = new List<string>(); foreach (var f in _coherentStates) { if(f.Name == "_coherentState") { var ftype = f.FieldType; var ssss= ftype.GetRuntimeFields(); foreach(var ss in ssss) { if (ss.Name == "_entries") { var eee = ss.GetValue(f.GetValue(_cache)); if (eee != null) { var cacheItems = eee as IDictionary; if (cacheItems == null) return keys; foreach (DictionaryEntry cacheItem in cacheItems) { if (!string.IsNullOrEmpty(key)) { if (cacheItem.Key.ToString().Contains(key)) { keys.Add(cacheItem.Key.ToString()); } } else { keys.Add(cacheItem.Key.ToString()); } } } } } } } return keys;