1

Having generic types like this:

SomeType<T>
SomeType<T1, T2>

and only have the type of them:

Type type1 = typeof(SomeType<T>);
Type type2 = typeof(SomeType<T1, T2>);

How to get the type of T from type1
and T1 and T2 from type2?

Example:
Using typeof(SomeType<int>) will only return SomeType´1.
But I'm searching for a way to get the int type, just as typeof(int) will do.

joe
  • 8,344
  • 9
  • 54
  • 80

2 Answers2

4

You should use GetGenericArguments method on Type class, that will return specific Types, that you have used to specify open generic types. Example for your case:

var type = typeof(SomeType<int, string>);

foreach(Type closedType in type.GetGenericArguments())
{
    Console.WriteLine(closedType);
}

where

class SomeType<T1, T2> {}

will print:

System.Int32
System.String
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
0

Depends on SomeType but I would suggest something like this:

Type innerType = type1.GetType().GetProperty("Item").PropertyType;
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • I guess the property `Item` means the indexer. But does this work when the `SomeType` does not implement a indexer? – joe Aug 06 '13 at 16:09