列出类'可用属性

时间:2012-08-19 19:28:26

标签: c# class

我正在连接到我写的服务,我想举例说明这项服务可以为我们的客户提供什么。

这就是我所拥有的:

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的内容以及该属性的名称及其值?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

var properties = cust.GetType().GetProperties();
foreach (var prop in properties)
{
    ListBox1.Items.Add(prop.Name + " = " + prop.GetValue(cust, null));
}