我创建了一个带有“XSD.exe”的Structure类,现在我遇到了一个简短的问题:
我在结构中有5个不同的“类”,所有5个类都具有相同的值:
public partial class Carrier
{
private string codeField;
private string companyField;
private string legalNameField;
private string addressField;
private string address2Field;
private string stateField;
private string cityField;
private string countryField;
private string phoneField;
private string faxField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Company
{
get
{
return this.companyField;
}
set
{
this.companyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LegalName
{
get
{
return this.legalNameField;
}
set
{
this.legalNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Address
{
get
{
return this.addressField;
}
set
{
this.addressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Address2
{
get
{
return this.address2Field;
}
set
{
this.address2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string State
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string City
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Phone
{
get
{
return this.phoneField;
}
set
{
this.phoneField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Fax
{
get
{
return this.faxField;
}
set
{
this.faxField = value;
}
}
}
我想将所有五个不同的对象设置为另一个对象:
private Adress FindAddresses(Carrier address)
{
tempAddress = new Address();
tempAddress.AddressCode = address.Code;
tempAddress.Name1 = address.LegalName;
tempAddress.Name2 = address.Address;
tempAddress.Name3 = address.Address2;
return tempAddress;
}
唯一的方法是将此功能重载5次或提供“更好”的方法吗?
答案 0 :(得分:2)
您可以让类实现相同的接口,例如IAdressable
:
public interface IAddressable
{
public AddressCode Code { get; set; }
public string LegalName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
}
现在该方法可以将IAddressable
作为参数:
private Adress FindAddresses(IAddressable address)
{
var tempAddress = new Address();
tempAddress.AddressCode = address.Code;
tempAddress.Name1 = address.LegalName;
tempAddress.Name2 = address.Address;
tempAddress.Name3 = address.Address2;
return tempAddress;
}
然后你可以用你的所有类调用这个方法,因为它们都实现了那个接口。
答案 1 :(得分:0)
您可以尝试将公共属性移动到公共基类中,并从该基类派生Carrier
和其他4个类。
然后,这些方法将获取基类的实例。
public class BaseAddressClass
{
...
}
public class Carrier : BaseAddressClass
{
// Other fields
}
private Address FindAddress(BaseAddressClass address)
{
...
}