2013-04-23 72 views
0

我打電話給一個webservice,返回四個自定義類之一的數組。 所有類都具有相同的內容 - 一個名爲Description的字符串和另一個名爲Value的字符串。 我想寫一個可以接受四個類中的任何一個的方法,並將其內容放入下拉列表的數據源中。從一個類轉換爲另一個

有沒有辦法將未知的組合類轉換爲具有相同內容的指定類?或者將內容刪除?

或者我將不得不用不同的數據類型來編寫四個相同的函數嗎?

編輯:添加代碼

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

這將是有益的,如果你提供一些代碼秒。 – 2013-04-23 06:15:42

+0

你可以使用'動態'。 – 2013-04-23 06:16:48

+0

你可以在所有類上放置一個接口並使用它。或者你可以使用AutoMapper – Brian 2013-04-23 06:17:53

回答

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 2013-04-23 06:42:53

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

這四個類是由Web服務定義的(這不是我寫的)。如果我需要刷新服務引用,我不想更改類定義,因爲那樣我就會失去我的修改。 – Adeptus 2013-04-23 06:30:35

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 
相關問題