14

有没有更明智的方法来执行以下操作:

public static class ValueTupleAdditions {
  public static IEnumerable<object> ToEnumerable<A, B>(this ValueTuple<A, B> tuple) {
    yield return tuple.Item1;
    yield return tuple.Item2;
  }
  public static IEnumerable<object> ToEnumerable<A, B, C>(this ValueTuple<A, B, C> tuple) {
    yield return tuple.Item1;
    yield return tuple.Item2;
    yield return tuple.Item3;
  }

  [etc]
}

编辑:因为人们要求一个用例,所以你去。

using Xunit;

namespace Whatever {

  public class SomeTestClass {
    public static IEnumerable<(string, Expression<Func<string, string>>, string)> RawTestData() {
      yield return ("Hello", str => str.Substring(3), "lo");
      yield return ("World", str => str.Substring(0, 4), "worl");
    }
    public static IEnumerable<object[]> StringTestData() {
      return RawTestData().Select(vt => new object[] { vt.Item1, vt.Item2, vt.Item3 });
       // would prefer to call RawTestData().Select(vt => vt.ToArray()) here, but it doesn't exist.
    }

    [Theory, MemberData(nameof(StringTestData))]
    public void RunStringTest(string input, Expression<Func<string, string>> func, string expectedOutput) {
      var output = func.Compile()(input);
      Assert.Equal(expectedOutput, output);
    }
  }
}
4

3 回答 3

3

一种方法是通过ITuple接口

public interface ITuple
{
    int Length { get; }
    object this[int index] { get; }
}

它仅在 .NET Core 2.0、Mono 5.0 和 .NET Framework 的下一版本(未发布,4.7 之后)中可用。它不(也永远不会)通过 ValueTuple 包作为旧框架的附加组件提供。

此 API 旨在供 C# 编译器使用,以用于未来的模式工作。

于 2017-08-01T19:30:41.310 回答
2

一点反思:

namespace ConsoleApp1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Program
    {
        public static void Main()
        {
            var tuple = (1, 2, 3, 4, 5, 6, 7);
            var items = ToEnumerable(tuple);

            foreach (var item in items)
            {
                Console.WriteLine(item);
            }
        }

        private static IEnumerable<object> ToEnumerable(object tuple)
        {
            if (tuple.GetType().GetInterface("ITupleInternal") != null)
            {
                foreach (var prop in tuple.GetType()
                    .GetFields()
                    .Where(x => x.Name.StartsWith("Item")))
                {
                    yield return prop.GetValue(tuple);
                }
            }
            else
            {
                throw new ArgumentException("Not a tuple!");
            }
        }
    }
}
于 2017-05-16T16:58:18.630 回答
2

一种方法是使用基于ITuple的扩展方法,另请参见Julien Couvreur 的回答

public static IEnumerable<T> ToEnumerable<T>( this ITuple tuple ) {
    for ( var n = 0; n < tuple.Length; n++ ) yield return (T)tuple[ n ];
}

示例用法:

var directions = (
    right: (cx: 1, cy: 0),
    down: (cx: 0, cy: 1),
    left: (cx: -1, cy: 0),
    up: (cx: 0, cy: -1)
);
foreach ( var direction in directions.ToEnumerable<(int cx, int cy)>() ) {
    var (cx, cy) = direction;
    TryMovePiece( (x + cx, y + cy) );
}
于 2021-11-12T16:08:03.107 回答