Helpers.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Linq.ObservableImpl;
  8. namespace System.Reactive
  9. {
  10. internal static class Helpers
  11. {
  12. public static int? GetLength<T>(IEnumerable<T> source)
  13. {
  14. var array = source as T[];
  15. if (array != null)
  16. return array.Length;
  17. var list = source as IList<T>;
  18. if (list != null)
  19. return list.Count;
  20. return null;
  21. }
  22. public static IObservable<T> Unpack<T>(IObservable<T> source)
  23. {
  24. var hasOpt = default(bool);
  25. do
  26. {
  27. hasOpt = false;
  28. var eval = source as IEvaluatableObservable<T>;
  29. if (eval != null)
  30. {
  31. source = eval.Eval();
  32. hasOpt = true;
  33. }
  34. } while (hasOpt);
  35. return source;
  36. }
  37. }
  38. }
  39. #endif