我最近开始使用 C#/Azure,并且有一个小问题想要解决。该应用程序按预期工作,但我想重构一堆类,因为我确信有一个更简单的解决方案。
我目前有一堆函数可以从 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 ...”尝试过,但这不起作用)。
最佳答案
你可以尝试这样的事情:
return (Entity)retrievedResult.Result;
class Entity : TableEntity
{
public string PartKey => PartitionKey;
public string RowKey => RowKey;
}
关于c# - 转换 TableOperation.Retrieve 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16836897/