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.
我有一个通用类foo。我有一个名为bar.
foo
bar
我如何获得类型foo<bar>?当您有对象的实例但我没有实例时,我已经看到使用工厂类型设置的示例。
foo<bar>
一些代码:
public class foo<T> { } public Type GetGenericFoo(Type bar) { Type genericFooBar = null; //work magic return genericFooBar; }
关于什么:
typeof(foo<>).MakeGenericType(bar)
所以在你的代码中它会像:
public Type GetGenericFoo(Type bar) { return typeof(foo<>).MakeGenericType(bar); }