All pastes #253687 Raw Edit

FlatRedBallServices

public text v1 · immutable
#253687 ·published 2006-11-21 00:12 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;

namespace FlatRedBall
{
    public static class FlatRedBallServices
    {

        private static Dictionary<Type, object> mPool;

        static FlatRedBallServices()
        {
            mPool = new Dictionary<Type, object>();
        }

        public static void RegisterService(object service)
        {
            Type t = service.GetType();
            RegisterService(t, service);
        }

        public static void RegisterService<T>(object service)
        {
            RegisterService(typeof(T), service);
        }

        public static void RegisterService(Type t, object service)
        {
            if (!mPool.ContainsKey(t))
            {
                mPool.Add(t, service);
            }
            else
            {
                mPool[t] = service;
            }
        }

        public static T GetService<T>()
        {
            if(mPool.ContainsKey(typeof(T)))
            {
                return (T)mPool[typeof(T)];
            }
            else
            {
                return default(T);
            }
        }
    }
}