SsdpDeviceExtensions.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Rssdp
  5. {
  6. /// <summary>
  7. /// Extensions for <see cref="SsdpDevice"/> and derived types.
  8. /// </summary>
  9. public static class SsdpDeviceExtensions
  10. {
  11. /// <summary>
  12. /// Returns the root device associated with a device instance derived from <see cref="SsdpDevice"/>.
  13. /// </summary>
  14. /// <param name="device">The device instance to find the <see cref="SsdpRootDevice"/> for.</param>
  15. /// <remarks>
  16. /// <para>The <paramref name="device"/> must be or inherit from <see cref="SsdpRootDevice"/> or <see cref="SsdpEmbeddedDevice"/>, otherwise an <see cref="System.InvalidCastException"/> will occur.</para>
  17. /// <para>May return null if the <paramref name="device"/> instance is an embedded device not yet associated with a <see cref="SsdpRootDevice"/> instance yet.</para>
  18. /// <para>If <paramref name="device"/> is an instance of <see cref="SsdpRootDevice"/> (or derives from it), returns the same instance cast to <see cref="SsdpRootDevice"/>.</para>
  19. /// </remarks>
  20. /// <returns>The <see cref="SsdpRootDevice"/> instance associated with the device instance specified, or null otherwise.</returns>
  21. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="device"/> is null.</exception>
  22. /// <exception cref="System.InvalidCastException">Thrown if <paramref name="device"/> is not an instance of or dervied from either <see cref="SsdpRootDevice"/> or <see cref="SsdpEmbeddedDevice"/>.</exception>
  23. public static SsdpRootDevice ToRootDevice(this SsdpDevice device)
  24. {
  25. if (device == null) throw new System.ArgumentNullException("device");
  26. var rootDevice = device as SsdpRootDevice;
  27. if (rootDevice == null)
  28. rootDevice = ((SsdpEmbeddedDevice)device).RootDevice;
  29. return rootDevice;
  30. }
  31. }
  32. }