[英]C# detect nullable reference type from generic argument of method's return type using reflection
[英]What are the usages of a generic method in C# without using type argument(<T>) as input parameters or return value?
C# 中泛型方法的用法是什么,而不使用類型參數作為輸入參數或返回值?
private void method1<T>()
剛剛在ASP.NET核心代碼中探索了一下,看到他們在很多地方都使用了這種泛型方法。
例如在 UseMiddlewareExtensions 中的 UseMiddleware。
public static IApplicationBuilder UseMiddleware<TMiddleware>(this IApplicationBuilder app,
params object[] args)
{
return app.UseMiddleware(typeof(TMiddleware), args);
}
這是一種常見的模式還是我錯了?
它只是一種傳入類型的方式,而不是添加額外的類型參數。 它與以下內容完全相同:
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app,
params object[] args,
Type middlewhereType)
{
return app.UseMiddleware(middlewhereType, args);
}
這只是風格選擇的問題,使用哪一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.