浏览起订量代码,我看到了这段代码。我希望有人可以解释对 Guard.NotNull 的调用中发生的情况,如下所示。这是来自 ExtensionExpression.cs,以防有人想知道
public static LambdaExpression ToLambda(this Expression expression)
{
Guard.NotNull(() => expression, expression);
/* other code suppressed...
/* code from Guard.cs */
internal static class Guard
{
/// <summary>
/// Ensures the given <paramref name="value"/> is not null.
/// Throws <see cref="ArgumentNullException"/> otherwise.
/// </summary>
public static void NotNull<T>(Expression<Func<T>> reference, T value)
{
if (value == null)
{
throw new ArgumentNullException(GetParameterName(reference));
}
}
/* rest of code suppressed */
最佳答案
它所做的只是在异常中返回参数的名称,而不是在值为 null 时对其进行硬编码。在 Moq 的情况下,参数的名称很可能来自测试本身,因此很难将其硬编码到 Moq 代码中。
表达式可以捕获有关方法/属性的信息,例如其名称。
关于c# - lambda表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768098/