繁体   English   中英

将泛型参数传递给具有特定继承的函数

[英]Passing a generic parameter into a function which has a specific inheritance

我有此功能基于customerIdentityModel进行查询。 我想使它适用于所有不同的模型。 换句话说,我想将CustomerFeatureEntity类作为参数传递给此函数:

public TableQuery<CustomerFeatureEntity> StorageQuery(string customerId)
{
    TableQuery<CustomerFeatureEntity> query = new TableQuery<CustomerFeatureEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, customerId));
}

如果我确保从TableEntity类继承该参数,那也很好。 是否可以使用C#完成其中之一?

是的,您只需要重新定义它即可使用具有where约束的泛型。

public TableQuery<T> StorageQuery<T>(string customerId) where T : TableEntity
{
    TableQuery<T> query = new TableQuery<T>()
        .Where(TableQuery.GenerateFilterCondition("PartitionKey", 
            QueryComparisons.Equal, customerId));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM