簡體   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