Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展方法没有';t按预期引发异常_C#_Generics_Extension Methods_Mstest - Fatal编程技术网

C# 扩展方法没有';t按预期引发异常

C# 扩展方法没有';t按预期引发异常,c#,generics,extension-methods,mstest,C#,Generics,Extension Methods,Mstest,: 公共静态T对象(此数据行),其中T:new() { 如果(row==null)抛出新的ArgumentNullException(“row”); //做点什么 } 公共静态IEnumerable对象(此数据表),其中T:new() { 如果(table==null)抛出新的ArgumentNullException(“table”); //做点什么 } 以及有关的测试: [TestMethod] [ExpectedException(typeof(ArgumentNullException

:

公共静态T对象(此数据行),其中T:new()
{
如果(row==null)抛出新的ArgumentNullException(“row”);
//做点什么
}
公共静态IEnumerable对象(此数据表),其中T:new()
{
如果(table==null)抛出新的ArgumentNullException(“table”);
//做点什么
}
以及有关的测试:

[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>();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
公共无效NullDataRow()
{
//安排
DataRow行=null;
//表演
row.ToObject();
}
[测试方法]
[ExpectedException(typeof(ArgumentNullException))]
公共无效NullDataTable()
{
//安排
DataTable=null;
//表演
table.ToObject();
}
通过(它通常抛出
ArgumentNullException
),而不通过(不命中方法也不抛出任何东西)

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


(起初我认为这是Visual Studio中的一个bug,但是)

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


这样的迭代器在开始迭代之前不会真正执行到引发异常的程度。

您在此处发布的代码不足以诊断问题,因为它是由隐藏在
///执行某些操作
注释后面的内容引起的。查看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);
}

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

dam,这很有意义。我完全忘记了延期执行。马上回来。
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);
}
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);
}