c# - 转换 TableOperation.Retrieve 的结果

标签 c# azure

我最近开始使用 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/

相关文章:

azure - 当 UseDevelopmentStorage=true 时,本地机器上的 Azure 跟踪日志在哪里

c# - Azure Graph API - 检查用户是否具有 O365 邮箱的完全访问权限

c# - 如何使用 ElasticSearch 只返回属性的子集

c# - 几何对象的可视化调试器

c# - 发布到 azure 时出现 System.NullReferenceException

azure - 政府云中 Azure 存储的 TLS

azure - 尝试通过主题/订阅接收 Azure 服务总线消息时出现序列化异常

c# - .Net Forms PropertyGrid 在对多个选定对象的属性进行排序时忽略 DisplayNameAttribute

c# - Android.Views.InflateException : Binary XML file line #1: Binary XML file line #1: Error inflating class android. support.v7.widget.DrawerLayout

c# - 如何在 C#/(ADO?).NET 2.0 中查询 (oracle) 数据库表结构