扩展方法不会按预期抛出异常

时间:2015-07-10 22:17:35

标签: c# generics extension-methods mstest

I have the following extension methods

public static T ToObject<T>(this DataRow row) where T : new()
{
    if (row == null) throw new ArgumentNullException("row");

    // do something
}

public static IEnumerable<T> ToObject<T>(this DataTable table) where T : new()
{
    if (table == null) throw new ArgumentNullException("table");

    // do something
}

各自的测试:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NullDataRow()
{
    // Arrange
    DataRow row = null;

    // Act
    row.ToObject<SomeData>();
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NullDataTable()
{
    // Arrange
    DataTable table = null;

    // Act
    table.ToObject<SomeData>();
}

DataRow test次传递(正常投掷ArgumentNullException),而DataTable one没有(不会点击方法也不投掷任何内容)。

我完全不知道为什么DataTable测试不起作用(而DataRow测试是正常的!)。

(起初我认为这是我的Visual Studio中的一个错误,但是the CI service that I use accused the same

2 个答案:

答案 0 :(得分:6)

我假设IEnumerable<T>不仅仅是为了好玩,而且扩展方法实际上是一个迭代器。

这样的迭代器,实际上并没有执行到抛出异常的程度,直到你开始迭代它。

答案 1 :(得分:1)

您在此处发布的代码不足以诊断问题,因为它是由/// do something评论背后隐藏的内容引起的。查看github上的代码:

public static IEnumerable<T> ToObject<T>(this DataTable table) where T : new()
{
    if (table == null) throw new ArgumentNullException("table");
    foreach (DataRow row in table.Rows) yield return ToObject<T>(row, false);
}

使yield return使得该方法在需要结果(执行迭代)之前不会实际执行任何代码。

使其工作遵循.NET团队在LINQ实现中使用的相同模式:

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector) {
    if (source == null) throw Error.ArgumentNull("source");
    if (selector == null) throw Error.ArgumentNull("selector");
    return SelectIterator<TSource, TResult>(source, selector);
}

其中SelectIterator使用yield return一次返回一个项目。