ArgumentException ParamName约定,用于多个参数和嵌套参数 - c#

考虑我有一个如下课程:

class ProductPrice
{
   public string ProductName { get; set; }
   public decimal RegularPrice { get; set; }
   public decimal SalePrice { get; set; }
}

我有这样的功能:

public decimal CalculateDiscount(ProductPrice thePriceInfo)
{
   if (thePriceInfo.SalePrice > thePriceInfo.RegularPrice)
      throw new ArgumentException("Sale price cannot be greater than regular price.","?????");

   return (thePriceInfo.RegularPrice-thePriceInfo.SalePrice) / thePriceInfo.RegularPrice;
}

我想知道当我调用ArgumentException的构造函数时应该为ParamName参数(上面的?????)添加什么。

我想知道以下情况下ParamName的标准约定是什么:

1)导致异常的参数嵌套在一个类中(即thePriceInfo.SalePrice)

2)例外是由于两个不同参数之间的相互作用(在这种情况下,SalePrice高于RegularPrice)。您用逗号或其他分隔它们吗?

此外,ParamName参数是否实际上可用于.NET本身或流行的第三方工具中的任何东西,还是仅供参考,以供其他调用代码使用?

参考方案

我不能代表可能使用该数据的第三方工具,但我认为没有任何约定。 MSDN表示它仅供参考:(对于最终用户,或对于可能在更上游的另一个程序集中捕获它的开发人员,以便他们可以相应地进行处理)

paramName的内容旨在为人类所理解。

因此,请设置您认为能传达正确信息的任何值。 IMO,正常价格没有错,但销售价格是正确的,因此“ SalePrice”或“ ProductPrice.SalePrice”都是如此。

如果我能观察一下...

您在这里实际上没有任何无效或超出范围的参数-仅指定一个数量的业务规则不能小于另一个。这不是例外,不需要例外。

我将您的逻辑分为两部分:一个验证价格,另一个计算折扣。

public bool IsDiscountValid(ProductPrice pp)
{
    return pp.SalePrice <= pp.RegularPrice;
}

public decimal CalculateDiscount(ProductPrice pp)
{
   return (pp.RegularPrice - pp.SalePrice) / pp.RegularPrice;
}

现在,调用者可以用最适合您程序的任何方式通知用户:

if (IsDiscountValid(thePriceInfo))
    return CalculateDiscount(thePriceInfo);

MessageBox.Show("Sale price cannot be greater than regular price.", "Invalid Price");

// or display text in a Label or whatever, depending on which platform you're using

.NET C#Webbrowser填充输入,不带ID或类名 - javascript

我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

asp.net mvc中的对象数组数据始终为null - javascript

我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…

ASP.NET MVC中应为DataTable返回哪种数据? - javascript

我想为DataTable中的每个页面创建动态加载信息。我正在尝试遵循以下示例:https://www.datatables.net/manual/server-sidehttps://www.datatables.net/manual/data来自示例的代码:$('#example').DataTable( { serverSide: t…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…