Пример #1
0
        /// <summary>
        /// Adds a child service to the <see cref="Services"/> collection.
        /// </summary>
        /// <param name="service">The <see cref="SsdpService"/> instance to add.</param>
        /// <remarks>
        /// <para>If the device is already a member of the <see cref="Services"/> collection, this method does nothing.</para>
        /// <para>Also sets the <see cref="SsdpService.RootDevice"/> property of the added device and all descendant devices to the
        /// relevant <see cref="SsdpRootDevice"/> instance.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="service"/> argument is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the <paramref name="service"/> is already associated with a different
        /// <see cref="SsdpRootDevice"/> instance than used in this tree. Can occur if you try to add the same device instance to more than
        /// one tree. Also thrown if you try to add a device to itself.</exception>
        public void AddService(SsdpService service)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            SsdpDevice thisRootDevice = GetRootDevice();

            if (service.RootDevice != null && !service.RootDevice.Equals(thisRootDevice))
            {
                throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch).");
            }

            if (service == this)
            {
                throw new InvalidOperationException("Can't add device to itself.");
            }

            lock (_services)
            {
                service.RootDevice = (SsdpRootDevice)thisRootDevice;
                if (!_services.Contains(service))
                {
                    _services.Add(service);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the root device of this object.
        /// </summary>
        /// <returns>Root device in the structure.</returns>
        public SsdpRootDevice GetRootDevice()
        {
            SsdpDevice?rootDevice = this switch
            {
                SsdpRootDevice root => root,
                SsdpService embedded => embedded.RootDevice,
                      _ => null
            };

            if (rootDevice == null)
            {
                throw new NullReferenceException("Root device cannot be null.");
            }

            return((SsdpRootDevice)rootDevice);
        }