2
0

SsdpDeviceExtensions.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Rssdp
  6. {
  7. /// <summary>
  8. /// Extensions for <see cref="SsdpDevice"/> and derived types.
  9. /// </summary>
  10. public static class SsdpDeviceExtensions
  11. {
  12. /// <summary>
  13. /// Returns the root device associated with a device instance derived from <see cref="SsdpDevice"/>.
  14. /// </summary>
  15. /// <param name="device">The device instance to find the <see cref="SsdpRootDevice"/> for.</param>
  16. /// <remarks>
  17. /// <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>
  18. /// <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>
  19. /// <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>
  20. /// </remarks>
  21. /// <returns>The <see cref="SsdpRootDevice"/> instance associated with the device instance specified, or null otherwise.</returns>
  22. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="device"/> is null.</exception>
  23. /// <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>
  24. public static SsdpRootDevice ToRootDevice(this SsdpDevice device)
  25. {
  26. if (device == null) throw new System.ArgumentNullException("device");
  27. var rootDevice = device as SsdpRootDevice;
  28. if (rootDevice == null)
  29. rootDevice = ((SsdpEmbeddedDevice)device).RootDevice;
  30. return rootDevice;
  31. }
  32. }
  33. }