4 개의 사용자 정의 클래스 중 하나의 배열을 반환하는 webservice를 호출합니다. 모든 클래스는 내부 내용이 동일합니다. Description이라는 문자열 하나와 Value라는 문자열이 있습니다. 4 개의 클래스 중 하나를 받아 들일 수있는 단일 메서드를 작성하고 해당 내용을 드롭 다운 목록의 데이터 소스에 넣으려고합니다.한 클래스에서 다른 클래스로 변환
알 수없는 복합 클래스를 동일한 내용의 지정된 클래스로 변환하는 방법이 있습니까? 또는 콘텐츠를 스트립합니까?
다른 데이터 유형으로 4 개의 동일한 함수를 작성해야합니까?
편집 : 추가 코드
myDropDown.DataSource = CreateDataSource(myWebServiceResponse.Items);
myDropDown.DataTextField = "DescriptionField";
myDropDown.DataValueField = "ValueField";
// Bind the data to the control.
myDropDown.DataBind();
...
public ICollection CreateDataSource(MasterData[] colData)
{
// Create a table to store data for the DropDownList control.
DataTable dt = new DataTable();
// Define the columns of the table.
dt.Columns.Add(new DataColumn("DescriptionField", typeof(String)));
dt.Columns.Add(new DataColumn("ValueField", typeof(String)));
// Populate the table
foreach (sapMasterData objItem in colData)
{
dt.Rows.Add(CreateRow(objItem, dt));
}
// Create a DataView from the DataTable to act as the data source
// for the DropDownList control.
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(MasterData objDataItem, DataTable dt)
{
// Create a DataRow using the DataTable defined in the
// CreateDataSource method.
DataRow dr = dt.NewRow();
dr[0] = objDataItem.Description;
dr[1] = objDataItem.Value;
return dr;
}
public class MasterData
{
public string Value;
public string Description;
}
당신은 몇 가지 코드를 제공하는 경우 그것은 도움이 될 것입니다 에스. –
'동적'을 사용할 수 있습니다. –
모든 클래스에 인터페이스를 추가하여 작업 할 수 있습니다. 아니면 AutoMapper – Brian