示例#1
0
        public IRegisteredObject CreateObject(string appId, Type type, string virtualPath,
                                              string physicalPath, bool failIfExists, bool throwOnError)
        {
            if (appId == null)
            {
                throw new ArgumentNullException("appId");
            }

            if (!VirtualPathUtility.IsAbsolute(virtualPath))
            {
                throw new ArgumentException("Relative path no allowed.", "virtualPath");
            }

            if (String.IsNullOrEmpty(physicalPath))
            {
                throw new ArgumentException("Cannot be null or empty", "physicalPath");
            }

            // 'type' is not checked. If it's null, we'll throw a NullReferenceException
            if (!typeof(IRegisteredObject).IsAssignableFrom(type))
            {
                throw new ArgumentException(String.Concat("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
            }

            //
            // ArgumentException is thrown for the physical path from the internal object created
            // in the new application domain.
            BareApplicationHost host = null;

            if (id_to_host.ContainsKey(appId))
            {
                host = id_to_host [appId];
            }

            IRegisteredObject ireg = null;

            if (host != null)
            {
                ireg = CheckIfExists(host, type, failIfExists);
                if (ireg != null)
                {
                    return(ireg);
                }
            }

            try {
                if (host == null)
                {
                    host = CreateHost(appId, virtualPath, physicalPath);
                }
                ireg = host.CreateInstance(type);
            } catch (Exception) {
                if (throwOnError)
                {
                    throw;
                }
            }

            if (ireg != null && host.GetObject(type) == null)              // If not registered from ctor...
            {
                host.RegisterObject(ireg, true);
            }

            return(ireg);
        }