.NET 7 MemoryCache GetProperty返回null

wn9m85ua  于 2023-10-21  发布在  .NET
关注(0)|答案(2)|浏览(161)

我有一个代码可以在.NET 6中正常工作,但是自从我升级到.NET 7代码就不工作了。
下面是代码:

// Get the empty definition for the EntriesCollection
        var cacheEntriesCollectionDefinition = typeof(MemoryCache).GetProperty("EntriesCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        if (cacheEntriesCollectionDefinition!=null)
        {
        
            // Populate the definition with your IMemoryCache instance.  
            // It needs to be cast as a dynamic, otherwise you can't
            // loop through it due to it being a collection of objects.
            var cacheEntriesCollection = cacheEntriesCollectionDefinition.GetValue(_cache) as dynamic;

            if (cacheEntriesCollection!=null)
            {
                // Define a new list we'll be adding the cache entries too
                List<ICacheEntry> cacheCollectionValues = new List<ICacheEntry>();

                foreach (var cacheItem in cacheEntriesCollection)
                {
                    // Get the "Value" from the key/value pair which contains the cache entry   
                    ICacheEntry cacheItemValue = cacheItem.GetType().GetProperty("Value").GetValue(cacheItem, null);

                    // Add the cache entry to the list
                    cacheCollectionValues.Add(cacheItemValue);
                }

                
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var keysToRemove = cacheCollectionValues.Where(d => regex.IsMatch(d.Key.ToString())).Select(d => d.Key).ToList();

                foreach (var key in keysToRemove)
                {
                    _cache.Remove(key);
                }
            }
        }

**typeof(MemoryCache).GetProperty(“EntriesCollection”)**始终返回null。我该怎么办?

esbemjvw

esbemjvw1#

MemoryCache has changed的内部设计。在GitHub上查看source code
如果你在一个 non-public 成员上有一个绑定,那么你必须在任何时候计算实现的变化,即使是在.NET的一个小更新完成的时候。
当这样的绑定对您来说是必须的时,最好开发一个单独的单元测试来验证这样的代码是否工作。

wh6knrhe

wh6knrhe2#

我很抱歉在很长一段时间后才回复这个,但我刚刚遇到了类似于此的版本错误。此代码将修复错误

var fieldInfo = typeof(MemoryCache).GetField("_coherentState", BindingFlags.Instance | BindingFlags.NonPublic);
var propertyInfo = fieldInfo.FieldType.GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.NonPublic);
var value = fieldInfo.GetValue(_memoryCache);
var dict = propertyInfo.GetValue(value) as dynamic;

List<ICacheEntry> cacheCollectionValues = new List<ICacheEntry>();

foreach (var cacheItem in dict)
{
    ICacheEntry cacheItemValue = cacheItem.GetType().GetProperty("Value").GetValue(cacheItem, null);
    cacheCollectionValues.Add(cacheItemValue);
}

var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = cacheCollectionValues.Where(d => regex.IsMatch(d.Key.ToString())).Select(d => d.Key).ToList();

foreach (var key in keysToRemove)
{
    _memoryCache.Remove(key);
}

相关问题