0

我正在连接我编写的一项服务,我想举例说明该服务可为我们的客户提供哪些服务。

这就是我所拥有的:

protected void GetCust(string accountnumber)
{
  var cust = _client.GetCustomer(CorpId, accountnumber);

  if (cust == null)
  {
    Label1.Visible = true;
    ListBox1.Items.Clear();
    return;
  }

  Label1.Visible = false;

  ListBox1.Items.Clear();
  ListBox1.Items.Add("AccountId = " + cust.AccountId);
  ListBox1.Items.Add("AreaCode = " + cust.AreaCode);
  ListBox1.Items.Add("BudgetBalance = " + cust.BudgetBalance);
  ListBox1.Items.Add("BudgetRate = " + cust.BudgetRate);
  ListBox1.Items.Add("CareOf = " + cust.CareOf);
  ListBox1.Items.Add("City = " + cust.City);
  ListBox1.Items.Add("CorporationId = " + cust.CorporationId);
  ListBox1.Items.Add("CreditCode = " + cust.CreditCode);
  ListBox1.Items.Add("CurrentBalance = " + cust.CurrentBalance);
  ListBox1.Items.Add("DefaultProductCode = " + cust.DefaultProductCode);
  ListBox1.Items.Add("DefaultUnitOfIssue = " + cust.DefaultUnitOfIssue);
  ListBox1.Items.Add("DeliveryCity = " + cust.DeliveryCity);
  ListBox1.Items.Add("DeliveryStreet = " + cust.DeliveryStreet);
  ListBox1.Items.Add("DepositAmount = " + cust.DepositAmount);
  ListBox1.Items.Add("FinanceChargeCode = " + cust.FinanceChargeCode);
  ListBox1.Items.Add("LastDeliveryDate = " + cust.LastDeliveryDate);
  ListBox1.Items.Add("LastPaymenRecievedDate = " + cust.LastPaymenRecievedDate);
  ListBox1.Items.Add("LastPaymentRecievedAmount = " + cust.LastPaymentRecievedAmount);
  ListBox1.Items.Add("MasterBillingAccount = " + cust.MasterBillingAccount);
  ListBox1.Items.Add("MasterBillingBranch = " + cust.MasterBillingBranch);
  ListBox1.Items.Add("Name = " + cust.Name);
  ListBox1.Items.Add("NonBudgetBalance = " + cust.NonBudgetBalance);
  ListBox1.Items.Add("NumberOfTanks = " + cust.NumberOfTanks);
  ListBox1.Items.Add("Over120 = " + cust.Over120);
  ListBox1.Items.Add("Over30 = " + cust.Over30);
  ListBox1.Items.Add("Over60 = " + cust.Over60);
  ListBox1.Items.Add("Over90 = " + cust.Over90);
  ListBox1.Items.Add("PercentFull = " + cust.PercentFull);
  ListBox1.Items.Add("PhoneNumber = " + cust.PhoneNumber);
  ListBox1.Items.Add("PreviousPercent = " + cust.PreviousPercent);
  ListBox1.Items.Add("PreviousStatmentBalance = " + cust.PreviousStatmentBalance);
  ListBox1.Items.Add("QuantityLastDelivered = " + cust.QuantityLastDelivered);
  ListBox1.Items.Add("SalesManCode = " + cust.SalesManCode);
  ListBox1.Items.Add("State = " + cust.State);
  ListBox1.Items.Add("Street = " + cust.Street);
  ListBox1.Items.Add("TankSerialNumber = " + cust.TankSerialNumber);
  ListBox1.Items.Add("TankSize = " + cust.TankSize);
  ListBox1.Items.Add("TankType = " + cust.TankType);
  ListBox1.Items.Add("TaxCode = " + cust.TaxCode);
  ListBox1.Items.Add("TotalBalance = " + cust.TotalBalance);
  ListBox1.Items.Add("TotalGasUsedLastYear = " + cust.TotalGasUsedLastYear);
  ListBox1.Items.Add("Type1 = " + cust.Type1);
  ListBox1.Items.Add("TypeCompanyTank = " + cust.TypeCompanyTank);
  ListBox1.Items.Add("YearToDateDeliveries = " + cust.YearToDateDeliveries);
  ListBox1.Items.Add("YearToDateGasDelivered = " + cust.YearToDateGasDelivered);
  ListBox1.Items.Add("ZipCode = " + cust.ZipCode);
  ListBox1.Items.Add("ZipCodeExtension = " + cust.ZipCodeExtension);
}

我的问题是,我不想每次进行更改时都必须手动更新它。

我的问题是,如何使用属性名称及其值列出 cust 的内容?

谢谢!

4

1 回答 1

1

您可以使用反射来获取类型中所有属性的列表(:

var properties = cust.GetType().GetProperties();
foreach (var prop in properties)
{
    ListBox1.Items.Add(prop.Name + " = " + prop.GetValue(cust, null));
}
于 2012-08-19T19:34:03.007 回答