」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > C#可以實現部分通用類型推理,如果是,如何?

C#可以實現部分通用類型推理,如果是,如何?

發佈於2025-03-22
瀏覽:132

Can C# Implement Partial Generic Type Inference, and If So, How?

Can Partial Generic Type Inference Be Implemented in C#?

Overview

This article explores the challenges of implementing type inference in C#, focusing on the limitations and potential solutions for partial generic type inference.

The Problem

The use描述的情況是特定基類應使用的擴展方法,其中具有與方法參數相關的通用參數。但是,擴展方法還應返回與所調用的特定後代相關的特定類型。

溶液

對於部分通用類型輸入

This approach requires specifying both generic type arguments during invocation, which may not be desirable in all cases.

Method 2: Using Two Functions with Wrapper

This approach involves splitting the operation into two functions:

public static thatwrapper { 返回新的whatwrapper (source); } 公共課程,這些公共課程 { // ... 執行 }

listoffruits.that()。是()。其中​​(banana => banana => banana => banana => banana => banana.peel!= color.black)
public static TReg Parameter(this TReg p, string name, T value)
    where TReg : ParameterizedRegistrationBase
通過將擴展方法直接引入基類,可以避免問題。但是,這種方法刪除了將來在其他類上使用擴展方法的能力。

部分通用類型的推廣在C#中不直接支持,但是有一些可提供類似功能的解決方法。使用的特定方法取決於所涉及的特定要求和權衡。
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3