public static T Create <T>(bool persistent = false, string name = null) where T : class, IService
        {
            var inst = Services.Get <T>();

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

            var tp = typeof(T);

            if (typeof(Component).IsAssignableFrom(tp))
            {
                return(ServiceComponent <T> .Create(tp, persistent, name));
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(tp))
            {
                return(ServiceScriptableObject <T> .Create(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);
                }
            }
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
0
        public static T CreateUnregisteredService <T>(bool persistent = false, string name = null) where T : class, IService
        {
            var tp = typeof(T);

            if (typeof(Component).IsAssignableFrom(tp))
            {
                return(ServiceComponent <T> .Create(tp, persistent, name));
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(tp))
            {
                return(ServiceScriptableObject <T> .Create(tp, persistent, name));
            }
            else
            {
                try
                {
                    return(System.Activator.CreateInstance <T>());
                }
                catch (System.Exception ex)
                {
                    throw new System.InvalidOperationException("Supplied concrete service type failed to construct.", ex);
                }
            }
        }