Thursday, August 30, 2007

NET: Checking what is the type argument

The following code sample show how you can check what is the type of the type argument passed to instantiate the generic type.


public class TypeDisplayer<T>
{
public string GetTypeArgument() { return typeof(T).FullName; }
}
 
static void ShowGenericTypeArgument()
{
//This will print System.Int32.
TypeDisplayer<int> i = new TypeDisplayer<int>();
Console.WriteLine(i.GetTypeArgument());
 
//This will print System.String.
TypeDisplayer<string> s = new TypeDisplayer<string>();
Console.WriteLine(s.GetTypeArgument());
}

Labels: , ,

0 Comments:

Post a Comment

<< Home