使用Azure存储客户端库3.0方法executequery <T>编译错误,其中T是tableentity

San*_*dke 3 azure-storage azure-table-storage c#-4.0

我必须遗漏一些明显的东西,但是以下失败并出现编译错误:

internal static IEnumerable<T> GetEntitiesWithCommaSeparatedRowKeys<T>(
string tableName, string partitionKey, 
string commaDelimitedStringOfRowKeys) where T: TableEntity
{
 ....
    TableQuery<T> entitiesQuery = new TableQuery<T>().Where(
                 TableQuery.CombineFilters(
                           TableQuery.GenerateFilterCondition("PartitionKey",
                           QueryComparisons.Equal, partitionKey),
                           TableOperators.And,
                           AzureHelper.GetFilterConditionForCommaDelimitedStringOfRowKeys(commaDelimitedStringOfRowKeys)
                           ));
    // compile error on this line
    IEnumerable<T> entities = table.ExecuteQuery<T>(entitiesQuery);
 ...
 }
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

'T' must be a non-abstract type with a public parameterless constructor 
in order to use it as parameter 'TElement' in the generic type or 
method 'Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteQuery<TElement>
(Microsoft.WindowsAzure.Storage.Table.TableQuery<TElement>, 
Microsoft.WindowsAzure.Storage.Table.TableRequestOptions, 
Microsoft.WindowsAzure.Storage.OperationContext)'
Run Code Online (Sandbox Code Playgroud)

TableEntity显然有一个公共无参数构造函数,并且是非抽象的.当我在TableEntity上点击F12时,以下是来自元数据信息的对象(只是为了确保正确解析TableEntity类型).

namespace Microsoft.WindowsAzure.Storage.Table
{

    public class TableEntity : ITableEntity
    {
        // Summary:
        //     Initializes a new instance of the Microsoft.WindowsAzure.Storage.Table.TableEntity
        //     class.
        public TableEntity();
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人的想法?仅供参考我正在使用Azure客户端库3.0.1.0.

更新:添加了解决类似问题的链接问题

San*_*dke 10

事实证明,如果方法提供类型约束,那么该约束也必须由该类型的任何调用者转发.

我不知道.

在这种情况下,Table.ExecuteQuery的定义如下所示

 public IEnumerable<TElement> ExecuteQuery<TElement>(TableQuery<TElement> query,
         TableRequestOptions requestOptions = null,
         OperationContext operationContext = null) 
                where TElement : ITableEntity, new();
Run Code Online (Sandbox Code Playgroud)

因此,在我的T约束中添加new()可以解决问题.

所以最终的方法声明看起来像

internal static IEnumerable<T> GetEntitiesWithCommaSeparatedRowKeys<T>(string tableName,
       string partitionKey,
       string commaDelimitedStringOfRowKeys) 
              where T : TableEntity , new() //This is new (pun intended :))
Run Code Online (Sandbox Code Playgroud)

在出现此问题的相关链接中找到了相关问题.

我猜测编译器总是可以在我实际调用时查找类型约束,但由于TableEntity是公共的(而不是密封的),我想它最终可能成为运行时问题.

另外有趣的是我的方法标记为内部,它应该真正使编译器能够检查库中的调用者.

无论如何,学到了新东西:).