2013-04-23 5 views
0

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; 
} 
+1

당신은 몇 가지 코드를 제공하는 경우 그것은 도움이 될 것입니다 에스. –

+0

'동적'을 사용할 수 있습니다. –

+0

모든 클래스에 인터페이스를 추가하여 작업 할 수 있습니다. 아니면 AutoMapper – Brian

답변

3

는 실제로 DropDownList 제어는 당신에게 필요 데이터 소스로 IEnumerable 다음을 지정할 수 DataTextFieldDataValueField :

dropDownList.DataSource = some_Array_You_Retrieved_From_Your_Web_Service; 
dropDownList.DataValueField = "Value"; 
dropDownList.DataTextField = "Description"; 
dropDownList.DataBind(); 

여러분이 볼 수 있듯이, 배열의 실제 타입은, ValueDescription 속성을 바인딩하는 한 실제로는 중요하지 않습니다.

+0

을 사용할 수 있습니다. 고맙습니다! – Adeptus

0

당신은 예를 들어, 모든 네 개의 클래스 (예를 들어 IDescriptionValue)에 의해 구현되는 인터페이스를 정의하고 해당 유형의 매개 변수를 받아들이는 방법을 쓸 수있다 :

public interface IDescriptionValue 
{ 
public string Description {get;set;} 
public string Value {get;set;} 
} 

public class CustomClass1 : IDescriptionValue{ 
public string Description {get;set;} 
public string Value {get;set;} 
} 
//snip... 
public class CustomClass4 : IDescriptionValue{ 
public string Description {get;set;} 
public string Value {get;set;} 
} 
//accepts parameters of type IDescriptionValue 
public void setDropdownData(IDescriptionValue inputData){ 
// your code here 
} 
+0

4 개의 클래스는 웹 서비스에 의해 정의됩니다 (저에게 쓴 것이 아닙니다). 서비스 참조를 새로 고침해야 할 경우를 대비하여 클래스 정의를 변경하고 싶지 않습니다. 수정을 잃어 버리기 때문입니다. – Adeptus

1

래퍼 할 것입니다, 당신은 두 가지 방법 :

public class WSData 
{ 
    public string Value; 
    public string Description; 

    // First approach: single ctor with dynamic parameter 
    public WSData(dynamic source) 
    { 
     this.Value = source.Value; 
     this.Description = source.Description; 
    } 

    // ----- or -------- 

    // Second approach: one ctor for each class 
    public WSData(FirstTypeFromWS source) 
    { 
     this.Value = source.Value; 
     this.Description = source.Description; 
    } 
    public WSData(SecondTypeFromWS source) 
    { 
     this.Value = source.Value; 
     this.Description = source.Description; 
    } 
} 

사용량이 동일합니다

WSData yourData = new WSData(data_retrieved_from_service); 
// now, bind the WSData object: you have abstracted yourself from 
// the service and as a bonus your code can be attached elsewhere more easily 
관련 문제