Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 C# 中,很容易创建一个开放的泛型类型,typeof(IEnumerable<>). 有没有办法创建一个包含开放泛型的类型?以下不起作用:typeof(IEnumerable<IFoo<>>)。
typeof(IEnumerable<>)
typeof(IEnumerable<IFoo<>>)
获取涉及的两个未绑定的泛型类型,然后使用一个作为参数从另一个创建泛型类型:
typeof(IEnumerable<>).MakeGenericType(typeof(IFoo<>))
不可能。这些不是通用但未绑定的类型,这些是特殊类型,它们不能被实例化。未绑定的泛型类型只能在直接类型表达式中使用。
使用时,typeof(IEnumerable<IFoo<>>)您不是在 typeof 表达式中使用未绑定类型,而是在IEnumerable<T>表达式中使用。
IEnumerable<T>