[英]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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.