简体   繁体   English

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

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

I have this function doing queries based on a customerId and entityModel . 我有此功能基于customerIdentityModel进行查询。 I want to make it generic for all different models. 我想使它适用于所有不同的模型。 In other words, I want to pass CustomerFeatureEntity class as a parameter into this function: 换句话说,我想将CustomerFeatureEntity类作为参数传递给此函数:

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

Also it would be nice if I make sure that parameter inheritance from TableEntity class. 如果我确保从TableEntity类继承该参数,那也很好。 Is either one of those possible to be done in C#? 是否可以使用C#完成其中之一?

Yes, you'd just need to redefine it to use generics with a where constraint. 是的,您只需要重新定义它即可使用具有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