How to get type name of argument of general type?

If I have a method signature, for example

public string myMethod<T>( ... ) 

How can I get the name of a type specified as a type argument inside a method? I would like to do something similar to typeof(T).FullName , but it really works ...

+43
generics c #
Apr 05 2018-10-05T00:
source share
3 answers

Your code should work. typeof(T).FullName excellent. This is a fully compiling, functioning program:

 using System; class Program { public static string MyMethod<T>() { return typeof(T).FullName; } static void Main(string[] args) { Console.WriteLine(MyMethod<int>()); Console.ReadKey(); } } 

Fulfillment of the above prints (as expected):

 System.Int32 
+72
Apr 05 '10 at 22:51
source share

typeof (T) .Name and typeof (T) .FullName work for me ... I get the type passed as an argument.

+2
Apr 05 '10 at 22:52
source share

Assuming you have some instance of T, it is no different from any other type.

 var t = new T(); var name = t.GetType().FullName; 
+1
Apr 05 '10 at 22:51
source share



All Articles