[英]How to check if a generic type definition inherits from another generic type definition
[英]How to get generic type from generic definition and generic arguments?
在C#中,我如何從泛型定義和泛型參數中構造泛型類型
var genericDefinition = typeof(List);
var genericArgument = typeof(string);
// How can I get the Type instance representing List<string> from the 2 variables above?
在我的用例中,泛型參數是動態解析的。 這可能在C#中嗎? 提前致謝。
沒有typeof(List)
這樣的東西。 但是, typeof(List<>)
工作正常,並且是開放的泛型類型。 然后你只需使用:
var genericDefinition = typeof(List<>);
var genericArgument = typeof(string);
var concreteListType = genericDefinition.MakeGenericType(new[] {genericArgument});
你會發現concreteListType
是typeof(List<string>)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.