csharp ObjectData扩展允许使用动态字典存储扩展任何.NET对象,GC将使用相关的ob收集它

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp ObjectData扩展允许使用动态字典存储扩展任何.NET对象,GC将使用相关的ob收集它相关的知识,希望对你有一定的参考价值。

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace ObjectDataUtils
{

    /// <summary>
    /// Allows to extend any .NET objects with dynamic dictionary storage,
    /// which will be collected by GC with the associated object 
    /// Latest code is here: https://gist.github.com/pmunin/b369f1e2555653970fc9
    /// </summary>
    public static class ObjectDataExtensions
    {
        static readonly ConditionalWeakTable<object, ConcurrentDictionary<string, object>> dataByObject = new ConditionalWeakTable<object, ConcurrentDictionary<string, object>>();

        /// <summary>
        /// Retrieves the data dictionary attached to the object. Dictionary is deallocated by GC when associated object is.
        /// </summary>
        /// <param name="target">extended object</param>
        /// <param name="createIfNotExist">create extension dictionary if it does not exist</param>
        /// <returns></returns>
        public static ConcurrentDictionary<string, object> Data(this object target, bool createIfNotExist = true)
        {
            var data = createIfNotExist ? dataByObject.GetOrCreateValue(target) : dataByObject.GetValue(target);
            return data;
        }

        /// <summary>
        /// Try get value from weak table and return default if it does not exist
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="conditionalWeakTable"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        static TValue GetValue<TKey, TValue>(this ConditionalWeakTable<TKey, TValue> conditionalWeakTable,
            TKey key, TValue defaultValue = default(TValue)) where TKey : class where TValue : class
        {
            TValue resValue = null;
            if (conditionalWeakTable.TryGetValue(key, out resValue))
                return resValue;
            return defaultValue;
        }

        /// <summary>
        /// Retrieves value by key attached to the object and null if value does not exist
        /// </summary>
        /// <param name="target"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Data(this object target, string key)
        {
            var objectData = target.Data(false);
            if (objectData == null) return null;
            object result = null;
            objectData.TryGetValue(key, out result);
            return result;
        }

        /// <summary>
        /// Sets add key value pair to the object's dynamic data
        /// </summary>
        /// <param name="target"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Data(this object target, string key, object value)
        {
            var objectData = target.Data();
            objectData[key] = value;
            return value;
        }
    }
}

以上是关于csharp ObjectData扩展允许使用动态字典存储扩展任何.NET对象,GC将使用相关的ob收集它的主要内容,如果未能解决你的问题,请参考以下文章

csharp 这为mvc创建了一个新的操作链接扩展,用于检查用户的声明并允许在声明有效时显示链接o

MVC上的jsonp扩展,解决跨域访问问题

是否存在允许动态增长和扩展 java.nio.ByteBuffer 的 ByteBuffer 实现?

Java 链表二叉树集合的相关总结(附代码)

动态水平可扩展键值存储

csharp 使用适当的缩进来扩展您的xml文档的扩展