C# 4.0 使用Azure存储客户端库3.0方法executequery编译错误<;T>;其中T是表实体
我肯定遗漏了一些明显的东西,但以下操作失败,出现编译错误:C# 4.0 使用Azure存储客户端库3.0方法executequery编译错误<;T>;其中T是表实体,c#-4.0,azure-storage,azure-table-storage,C# 4.0,Azure Storage,Azure Table Storage,我肯定遗漏了一些明显的东西,但以下操作失败,出现编译错误: internal static IEnumerable<T> GetEntitiesWithCommaSeparatedRowKeys<T>( string tableName, string partitionKey, string commaDelimitedStringOfRowKeys) where T: TableEntity { .... TableQuery<T> entit
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);
...
}
有什么想法吗?
仅供参考,我正在使用Azure客户端库3.0.1.0
更新:添加了一个解决了类似问题的方法,结果表明,如果一个方法提供了对类型的约束,那么该约束也必须由该类型的任何调用方转发 我不知道 在本例中,Table.ExecuteQuery的定义如下所示
public IEnumerable<TElement> ExecuteQuery<TElement>(TableQuery<TElement> query,
TableRequestOptions requestOptions = null,
OperationContext operationContext = null)
where TElement : ITableEntity, new();
public IEnumerable ExecuteQuery(TableQuery,
TableRequestOptions requestOptions=null,
OperationContext操作上下文=空)
其中TElement:ITableEntity,new();
因此,将new()添加到我的T约束中可以解决这个问题
最后的方法声明如下
internal static IEnumerable<T> GetEntitiesWithCommaSeparatedRowKeys<T>(string tableName,
string partitionKey,
string commaDelimitedStringOfRowKeys)
where T : TableEntity , new() //This is new (pun intended :))
内部静态IEnumerable GetEntities和CommaseParatedRowKeys(字符串表名,
字符串分区键,
字符串commaDelimitedStringOfRowKeys)
其中T:TableEntity,new()//这是新的(双关语:)
在出现此问题的其中一个中发现了相关问题
我猜编译器总是可以在我实际进行调用时查找类型约束,但是由于TableEntity是公共的(而不是密封的),我猜它最终可能会成为一个运行时问题
还值得注意的是,我的方法被标记为internal,这应该使编译器能够检查库中的调用方
总之,我学到了一些新东西:)。谢谢分享,是的,它很管用。new()用于初始化TElement泛型的无参数构造函数,该构造函数必须是TableEntity类型。这就是为什么在定义从TableEntity继承的类时需要使用无参数构造函数的原因。你帮我省了一些时间,谢谢。
public IEnumerable<TElement> ExecuteQuery<TElement>(TableQuery<TElement> query,
TableRequestOptions requestOptions = null,
OperationContext operationContext = null)
where TElement : ITableEntity, new();
internal static IEnumerable<T> GetEntitiesWithCommaSeparatedRowKeys<T>(string tableName,
string partitionKey,
string commaDelimitedStringOfRowKeys)
where T : TableEntity , new() //This is new (pun intended :))