DescriptorUtil.cs 1.0 KB

123456789101112131415161718192021222324252627
  1. using System.Collections.Generic;
  2. using Google.ProtocolBuffers.Collections;
  3. namespace Google.ProtocolBuffers.Descriptors {
  4. /// <summary>
  5. /// Internal class containing utility methods when working with descriptors.
  6. /// </summary>
  7. static class DescriptorUtil {
  8. /// <summary>
  9. /// Equivalent to Func[TInput, int, TOutput] but usable in .NET 2.0. Only used to convert
  10. /// arrays.
  11. /// </summary>
  12. internal delegate TOutput IndexedConverter<TInput, TOutput>(TInput element, int index);
  13. /// <summary>
  14. /// Converts the given array into a read-only list, applying the specified conversion to
  15. /// each input element.
  16. /// </summary>
  17. internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput>(IList<TInput> input,
  18. IndexedConverter<TInput, TOutput> converter) {
  19. TOutput[] array = new TOutput[input.Count];
  20. for (int i = 0; i < array.Length; i++) {
  21. array[i] = converter(input[i], i);
  22. }
  23. return Lists<TOutput>.AsReadOnly(array);
  24. }
  25. }
  26. }