Пример #1
0
        public static T GetOrCreate <T>(bool persistent = false, string name = null) where T : class, IService
        {
            var inst = Services.Get <T>();

            if (inst != null)
            {
                return(inst);
            }

            var tp = typeof(T);

            if (typeof(Component).IsAssignableFrom(tp))
            {
                return(ServiceComponent <T> .GetOrCreate(tp, persistent, name));
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(tp))
            {
                return(ServiceScriptableObject <T> .GetOrCreate(tp, persistent, name));
            }
            else
            {
                try
                {
                    var obj = System.Activator.CreateInstance <T>();
                    Services.Register <T>(obj);
                    return(obj);
                }
                catch (System.Exception ex)
                {
                    throw new System.InvalidOperationException("Supplied concrete service type failed to construct.", ex);
                }
            }
        }
        public static TConcrete Create <TServiceType, TConcrete>(bool persistent = false, string name = null) where TServiceType : class, IService
            where TConcrete : class, TServiceType
        {
            var inst = Services.Get <TServiceType>();

            if (inst != null)
            {
                throw new System.InvalidOperationException(string.Format("A service of type '{0}' already exists.", typeof(TServiceType).Name));
            }

            var tp = typeof(TConcrete);

            if (typeof(Component).IsAssignableFrom(tp))
            {
                return(ServiceComponent <TServiceType> .Create(tp, persistent, name) as TConcrete);
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(tp))
            {
                return(ServiceScriptableObject <TServiceType> .Create(tp, persistent, name) as TConcrete);
            }
            else
            {
                try
                {
                    var obj = System.Activator.CreateInstance <TConcrete>();
                    Services.Register <TServiceType>(obj);
                    return(obj);
                }
                catch (System.Exception ex)
                {
                    throw new System.InvalidOperationException("Supplied concrete service type failed to construct.", ex);
                }
            }
        }
Пример #3
0
        public static TServiceType GetOrCreate <TServiceType>(System.Type concreteType, bool persistent = false, string name = null) where TServiceType : class, IService
        {
            if (concreteType == null)
            {
                throw new System.ArgumentNullException("concreteType");
            }
            if (!typeof(TServiceType).IsAssignableFrom(concreteType))
            {
                throw new System.ArgumentException("Type must implement " + typeof(TServiceType).Name);
            }

            var inst = Services.Get <TServiceType>();

            if (inst != null)
            {
                return(inst);
            }

            if (typeof(Component).IsAssignableFrom(concreteType))
            {
                var obj = ServiceComponent <TServiceType> .Create(concreteType, persistent, name);

                Services.Register <TServiceType>(obj);
                return(obj);
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(concreteType))
            {
                var obj = ServiceScriptableObject <TServiceType> .Create(concreteType, persistent, name);

                Services.Register <TServiceType>(obj);
                return(obj);
            }
            else
            {
                try
                {
                    var obj = System.Activator.CreateInstance(concreteType) as TServiceType;
                    Services.Register <TServiceType>(obj);
                    return(obj);
                }
                catch (System.Exception ex)
                {
                    throw new System.InvalidOperationException("Supplied concrete service type failed to construct.", ex);
                }
            }
        }