I have created a Structure class with the "XSD.exe" and now I have a short problem:
我用“XSD.exe”创建了一个Structure类,现在我遇到了一个简短的问题:
I have 5 different "Classes" in the Structure and all 5 Classes have the same values like this:
我在结构中有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;
}
}
}
I want set all five different object to another object like this:
我想将所有五个不同的对象设置为另一个对象,如下所示:
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;
}
Is the only way to do this to overload this function 5 times or gives an "better" way to do this ?
唯一的方法是将此函数重载5次或者提供“更好”的方法来执行此操作?
2 个解决方案
#1
You could let the classes implement the same interface, for example IAdressable
:
您可以让这些类实现相同的接口,例如IAdressable:
public interface IAddressable
{
public AddressCode Code { get; set; }
public string LegalName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
}
Now the method could take an IAddressable
as argument:
现在该方法可以采用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;
}
Then you can call this method with all your classes since they all implement that interface.
然后,您可以使用所有类调用此方法,因为它们都实现了该接口。
#2
You could try to move the common properties into a common base class and derive Carrier
and the 4 other classes from that base class.
您可以尝试将公共属性移动到公共基类中,并从该基类派生Carrier和其他4个类。
The methods would then take an instance of the base class.
然后,这些方法将获取基类的实例。
public class BaseAddressClass
{
...
}
public class Carrier : BaseAddressClass
{
// Other fields
}
private Address FindAddress(BaseAddressClass address)
{
...
}
#1
You could let the classes implement the same interface, for example IAdressable
:
您可以让这些类实现相同的接口,例如IAdressable:
public interface IAddressable
{
public AddressCode Code { get; set; }
public string LegalName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
}
Now the method could take an IAddressable
as argument:
现在该方法可以采用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;
}
Then you can call this method with all your classes since they all implement that interface.
然后,您可以使用所有类调用此方法,因为它们都实现了该接口。
#2
You could try to move the common properties into a common base class and derive Carrier
and the 4 other classes from that base class.
您可以尝试将公共属性移动到公共基类中,并从该基类派生Carrier和其他4个类。
The methods would then take an instance of the base class.
然后,这些方法将获取基类的实例。
public class BaseAddressClass
{
...
}
public class Carrier : BaseAddressClass
{
// Other fields
}
private Address FindAddress(BaseAddressClass address)
{
...
}