Пример #1
0
        public static void ProvideService(Type type, bool lazyInitialize = true)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            initializedServices.TryRemove(type, out _);
            foreach (KeyValuePair <Type, dynamic> kvp in lookupCache)
            {
                if (ReflectUtil.DoesExtend(kvp.Key, type))
                {
                    lookupCache.TryRemove(kvp.Key, out _);
                }
            }

            int index = services.IndexOf(type);

            if (index > -1)
            {
                services[index] = type;
            }
            else
            {
                services.Add(type);
            }

            if (!lazyInitialize)
            {
                initializedServices.TryAdd(type, initializeService(type));
            }
        }
Пример #2
0
        public static List <T> RemoveServices <T>()
        {
            Type     type   = typeof(T);
            List <T> retVal = new List <T>();

            foreach (KeyValuePair <Type, dynamic> kvp in lookupCache)
            {
                if (ReflectUtil.DoesExtend(kvp.Key, type))
                {
                    lookupCache.TryRemove(kvp.Key, out _);
                }
            }

            for (int i = services.Count - 1; i >= 0; i--)
            {
                Type t = services[i];
                if (ReflectUtil.DoesExtend(type, t))
                {
                    dynamic result = null;
                    initializedServices.TryRemove(t, out result);
                    if (result != null)
                    {
                        retVal.Add((T)result);
                    }
                    services.RemoveAt(i);
                }
            }

            return(retVal);
        }
Пример #3
0
        //public
        public void AddState(Type state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }
            if (!ReflectUtil.DoesExtend(typeof(FiniteState), state))
            {
                throw new Exception("state does not extend FiniteState.");
            }

            FiniteState ns = null;

            try {
                ns = (FiniteState)Activator.CreateInstance(state, this);
            } catch (Exception ex) {
                throw new Exception("Cannot create state.", ex);
            }

            FiniteState oldState = null;

            if (states.TryGetValue(state, out oldState))
            {
                if (currentState == oldState)
                {
                    currentState = states[state] = ns;
                    currentState.Enter();
                }
                else
                {
                    states[state] = ns;
                }
                if (ReflectUtil.DoesExtend(typeof(IDisposable), oldState.GetType()))
                {
                    ((IDisposable)oldState).Dispose();
                }
            }
            else
            {
                states.Add(state, ns);
            }

            if (currentState == null)
            {
                currentState = ns;
                currentState.Enter();
            }
        }
Пример #4
0
        //public
        public static T GetService <T>()
        {
            Type    type   = typeof(T);
            dynamic result = null;

            if (!initializedServices.TryGetValue(type, out result))
            {
                int index = services.IndexOf(type);
                if (index > -1)
                {
                    result = initializeService(type);
                    initializedServices.TryAdd(type, result);
                }
            }

            if (result == null)
            {
                lookupCache.TryGetValue(type, out result);
            }

            if (result == null)
            {
                for (int i = 0; i < services.Count; i++)
                {
                    Type t = services[i];
                    if (ReflectUtil.DoesExtend(type, t))
                    {
                        initializedServices.TryGetValue(t, out result);
                        if (result == null)
                        {
                            result = initializeService(t);
                            initializedServices.TryAdd(t, result);
                        }
                        lookupCache.TryAdd(type, result);
                        break;
                    }
                }
            }

            if (result == null)
            {
                return(default(T));
            }
            else
            {
                return((T)result);
            }
        }