简体   繁体   中英

List Class' Available Properties

I am connecting to a service that I wrote and I want to give an example of what is available on this service for our customers.

This is what I have:

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);
}

My problem is, I don't want to have to update this manually every time that I make a change.

My question is, how do I list the contents of cust with the name of the property and it's value?

Thanks!

You can use reflection to get a list of all the properties in your type (:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM