If I have a method signature like
public string myMethod<T>( ... )
How can I, inside the method, get the name of the type that was given as type argument? I'd like to do something similar to typeof(T).FullName, but that actually works...
If I have a method signature like
public string myMethod<T>( ... )
How can I, inside the method, get the name of the type that was given as type argument? I'd like to do something similar to typeof(T).FullName, but that actually works...
Your code should work. typeof(T).FullName is perfectly valid. 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();
}
}
Running the above prints (as expected):
System.Int32
Assuming you have some instance of a T available, it's no different than any other type.
var t = new T();
var name = t.GetType().FullName;
typeof(T).Name and typeof(T).FullName are working for me...I get the type passed as argument.