I am working with three different classes that have a common interface. However, the interface, does not include all of the methods and properties for all three. Therefore, I am stuck with a situation where I have code duplication, see below:
if (party is Entity)
{
var entity = party as Entity;
txtName1.Text = entity.Name1;
Address addr = entity.Addresses.FindPrimary();
txtAddr1.Text = addr.Address1;
txtCity.Text = addr.City;
txtZip.Text = addr.Zip;
}
else if (party is CustomerAccount)
{
var acct = party as CustomerAccount;
txtName1.Text = acct.Name1;
Address addr = acct.Addresses.FindPrimary();
txtAddr1.Text = addr.Address1;
txtCity.Text = addr.City;
txtZip.Text = addr.Zip;
}
else if (party is CustomerQuote)
{
var quote = party as CustomerQuote;
txtName1.Text = quote.Name1;
Address addr = quote.Addresses.FindPrimary();
txtAddr1.Text = addr.Address1;
txtCity.Text = addr.City;
txtZip.Text = addr.Zip;
}
Is there a way to create a generic method to handle this situation. Something like the following:
private void getAddressInfo<T>(T party)
{
var sharedType = party as party.GetType();
txtName1.Text = sharedType.Name1;
Address addr = sharedType.Addresses.FindPrimary();
txtAddr1.Text = addr.Address1;
txtCity.Text = addr.City;
txtZip.Text = addr.Zip;
}