2013-05-30 21 views
9

我最近進入了C#/ Azure,並且遇到了一個我想解決的小問題。該應用程序按預期工作,但我想重構一堆類,因爲我確信有一個更簡單的解決方案。施放TableOperation的結果.Retrieve

我現在有一組函數來檢索Azure的,只有在被檢索的類型有所不同的實體,但最佳我只想要一個類,像這樣:

public static Object Do(string RowKey, string partitionKey, string tableName) 
{ 
    var theTable = Connect.Initialize(tableName); 
    var retrieveOperation = TableOperation.Retrieve(
             Base64.EncodeTo64(partitionKey), 
             Base64.EncodeTo64(RowKey)); 

    var retrievedResult = theTable.Execute(retrieveOperation); 

    if (retrievedResult.Result != null) { 
     return retrievedResult.Result; 
    } 
    throw new ArgumentException(
       String.Format("{0} not found in the Table", RowKey)); 
} 

這本身,工作並檢索所需的實體。但是,我無法在沒有錯誤的情況下投射返回的對象。

我想將它轉換爲的對象類型實現TableEntity-Type並將結果與​​表格匹配。

我知道我可以施展它的

TableOperation.Retrieve<Type>(...) 

感,但我真的想用一個單一的功能用於此目的,需要我丟在調用該函數。

我懷疑這個問題與結果是DynamicTableEntity類型的事實有關,但我爲什麼會迷失方向。

有沒有什麼辦法可以很好地解決這個問題/有沒有辦法建立一個參數來保存我想要的類型作爲結果? (我用「Type ...」嘗試過,但不起作用)。

+3

你有沒有嘗試使用通用類型?如: public static T Do(...){ ... TableOperation.Retrieve (...) } –

+0

此API非常荒謬。 – Rookian

回答

-4

您可以使用泛型類型的功能在C#

參考this link

您也可以參考的計算器例子here

0

你可以嘗試這樣的事:

return (Entity)retrievedResult.Result; 

class Entity : TableEntity 
{ 
    public string PartKey => PartitionKey; 
    public string RowKey => RowKey; 
}