【问题标题】:IEnumerable extension method issueIEnumerable 扩展方法问题
【发布时间】:2013-09-18 12:10:04
【问题描述】:

我有这个从 Internet 下载的 IEnumerable 扩展方法,我正在努力编写一个有效的回顾性测试

扩展方法

public static bool In<T>(this T source, params T[] list) where T : IEnumerable
{
    if (source == null) throw new ArgumentNullException("source");
    return list.Contains(source);
}

测试

[TestMethod]
public void In()
{

    IEnumerable<int> search = new int[] { 0 };
    IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };

    Assert.IsTrue(search.In(found));
    Assert.IsFalse(search.In(notFound));
}

结果

所有代码都可以编译,但在两个断言中,当我相信第一个断言 search.In(found) 应该返回 true 时,扩展方法的结果返回 false。

问题

我在调用代码中做错了什么还是扩展有问题?

【问题讨论】:

    标签: c# .net extension-methods ienumerable


    【解决方案1】:

    要进行此测试,应如下所示:

    [TestMethod]
    public void In()
    {
    
        IEnumerable<int> search = new int[] { 0 };
        IEnumerable<int> a = new int[] { 0, 0, 1, 2, 3 };
        IEnumerable<int> b = new int[] { 0, 0, 1, 2, 3 };
        IEnumerable<int> c = new int[] { 0};
    
        Assert.IsTrue(search.In(a,b,c));
        Assert.IsFalse(search.In(a,b));
    }
    

    这是因为您在这里搜索整个 int 数组,而不是其中的项目

    在上面的代码中,您正在检查所有传递的参数中是否有您正在搜索的数组

    如果你想要检查项目是否在列表中的扩展,试试这个:

    public static bool In<T> (this T value, IEnumerable<T> list)
    {
         return list.Contains(value);
    }
    

    然后你可以这样做:

    [TestMethod]
    public void In()
    {
    
        IEnumerable<int> search = 0;
        IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
        IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };
    
        Assert.IsTrue(search.In(found));
        Assert.IsFalse(search.In(notFound));
    }
    

    编辑:

    感谢马辛的评论

    【讨论】:

    • @MarcinJuraszek 哎呀
    【解决方案2】:

    您的T 在这里是IEnumerable&lt;int&gt;,而不是int。将局部变量键入为int[]。现在,您在 params 参数槽中使用单个 IEnumerable&lt;int&gt; 调用 In

    再看看你那里的扩展是假的。为什么源和搜索项(params 参数)都应该是IEnumerable?没有意义。这是一个工作版本:

        public static bool In<T>(this T operand, params T[] values) where T : IEquatable<T>
        {
            if (values == null) throw new ArgumentNullException("operand");
    
            return In(operand, ((IEnumerable<T>)values));
        }
        public static bool In<T>(this T operand, IEnumerable<T> values) where T : IEquatable<T>
        {
            if (values == null) throw new ArgumentNullException("operand");
    
            return values.Contains(operand);
        }
    

    【讨论】:

    • 在第一个断言失败的情况下给我同样的失败
    • 要确保,请尝试In&lt;int&gt;。如果您的代码正确,那应该可以工作,否则编译失败。
    【解决方案3】:

    如果你想查看source 中的所有元素都在list 中的天气:

        public static bool In<T>(this IEnumerable<T> source, IEnumerable<T> list)
        {
            if (source == null) throw new ArgumentNullException("source");
            return source.All(e => list.Contains(e));
        }
    
        IEnumerable<int> search = new int[] { 0 };
        IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
        IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };
    
        Console.WriteLine(search.In(found));//True
        Console.WriteLine(search.In(notFound));//False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2016-02-01
      相关资源
      最近更新 更多