Esempio n. 1
0
        public DynData(TTarget obj, bool keepAlive)
        {
            if (obj != null)
            {
                WeakReference weak = new WeakReference(obj);

                // Ideally this would be a "no GC region", but that's too new.
                CreationsInProgress++;
                lock (_DataMap) {
                    if (!_DataMap.TryGetValue(weak, out _Data))
                    {
                        _Data = new _Data_();
                        _DataMap.Add(weak, _Data);
                    }
                }
                CreationsInProgress--;

                Weak = weak;
                if (keepAlive)
                {
                    KeepAlive = obj;
                }
            }
            else
            {
                _Data = _DataStatic;
            }

            OnInitialize?.Invoke(this, obj);
        }
Esempio n. 2
0
        public static _Data_ <TTarget> For(TTarget obj)
        {
            WeakReference weak = new WeakReference(obj);

            if (ObjectMap.TryGetValue(weak, out _Data_ <TTarget> data))
            {
                return(data);
            }

            data = new _Data_ <TTarget>(obj, weak);
            ObjectMap.Add(weak, data);
            return(data);
        }
Esempio n. 3
0
 public DynData(TTarget obj)
 {
     if (obj != null)
     {
         WeakReference weak = new WeakReference(obj);
         lock (_DataMap) {
             if (!_DataMap.TryGetValue(weak, out _Data))
             {
                 _Data = new _Data_();
                 _DataMap.Add(weak, _Data);
             }
         }
         Weak = weak;
     }
     else
     {
         _Data = _DataStatic;
     }
     OnInitialize?.Invoke(this, obj);
 }
Esempio n. 4
0
        public DynamicData(Type type, object obj, bool keepAlive)
        {
            TargetType = type;

            lock (_CacheMap) {
                if (!_CacheMap.TryGetValue(type, out _Cache))
                {
                    _Cache = new _Cache_(type);
                    _CacheMap.Add(type, _Cache);
                }
            }

            if (obj != null)
            {
                lock (_DataMap) {
                    if (!_DataMap.TryGetValue(obj, out _Data))
                    {
                        _Data = new _Data_(type);
                        _DataMap.Add(obj, _Data);
                    }
                }

                Weak = new WeakReference(obj);
                if (keepAlive)
                {
                    KeepAlive = obj;
                }
            }
            else
            {
                lock (_DataStaticMap) {
                    if (!_DataStaticMap.TryGetValue(type, out _Data))
                    {
                        _Data = new _Data_(type);
                        _DataStaticMap.Add(type, _Data);
                    }
                }
            }

            OnInitialize?.Invoke(this, type, obj);
        }