Get类型对象的泛型类型(没有对象的实例存在)

问题描述:

我有一个泛型类foo。我有一个Type对象barGet类型对象的泛型类型(没有对象的实例存在)

如何获取类型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); 
} 
+1

它应该是'typeof运算(富 )'。 – 2012-08-10 14:08:31

+0

'typeof(foo)'应该是'typeof(foo )' – hvd 2012-08-10 14:08:36

+0

Si ..更正,谢谢.. – 2012-08-10 14:09:04