How do I get the type name of a generic type argument?

Related searches

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)



Running the above prints (as expected):


Generic Types (The Java™ Tutorials > Learning the Java Language , The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, , and Tn. Here is a function that will accept any type (assuming it has a base class with generic parameters) and return the types of that instance's base class's generic parameters: public Type[] BaseGenericTypes<T>(T instance) { Type instanceType = instance.GetType(); Type objectType = new object().GetType(); while (instanceType.BaseType != objectType) { instanceType = instanceType.BaseType; } return instanceType.GetGenericArguments(); }

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

Type.GetGenericTypeDefinition Method (System), Returns a Type object that represents a generic type definition from which the type arguments, the GetGenericTypeDefinition method returns the same Type� In case you need the type T of a generic type like List<T> you can use something like this t.GetGenericArguments()[0].Name. I needed this a while ago and couldn't find it anywhere. This will return string in the case you have a List<string> – Felipe Correa Mar 31 '16 at 0:27

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;

How to: Examine and Instantiate Generic Types with Reflection , To examine a generic type and its type parameters The following code displays the name and parameter position of a Type object that� As pointed out by Jon Sharpe in his comment on your question, the correct approach, both in terms of type safety and expressiveness, is to declare this assumption as a type constraint, making it explicit and known to the type checker. The way to do this is to use the type constraint syntax on your generic type parameter. For example, you could

Generics: Type Parameters, However, the name does not conflict with a name of the same spelling used in non-type contexts (such as the names of a class constant, an attribute, a method, an� This is almost certainly a flaw in the design of your function if you need to get the type of the generic type parameter; This is opposite of “generic”. Hence, use overloading instead. Other than that, Unsliced has already given the correct answer of determining the type of T.

Suggestion: typeof of generic type � Issue #204 � microsoft , Generic type parameters are types; they are already types and there's no need A class definition introduces a name in the type space that refers to the type of� For a given type, I want to find the generic arguments which a class imlements via IGeneric. It is more clear in this example: Class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { } Type t = typeof(MyClass); Type[] typeArgs = GetTypeArgsOfInterfacesOf(t); // At this point, typeArgs must be equal to { typeof (Employee), typeof (Company) }

A generic class, such as GenericList<T> listed in Introduction to Generics, cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use GenericList<T>, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this

  • typeof(T).FullName should work. What's happening instead?
  • I got compiler errors on that statement - but apparently they were caused by something else, because now it's working. Thanks!
  • Make sure to test it with MyMethod<int>>() and see what you have to account for nullable types if you care for the underlying type in that scenario.
  • You mean "<int?>" If so, it works, but you get System.Nullable<int> (in full name syntax), which is what you'd expect...
  • Even though I already had the solution (although it didn't work for some reason...), I'll give you the rep points for writing the best answer by far =)
  • This just helped me since I assumed that nameof(T) and typeof(T).Name would do the same thing. Turns out nameof(T) just returns T.
  • ah. If the type you passed is Nullable, to get the underlying type you'd have to use something like typeof (T).GetGenericArguments()[0]
  • to check if the type if nullable, you'd use typeof(T).IsGenericType, and if it is, you'd use the following to get the Name or FUllName ((Type)typeof(T).GetGenericArguments()[0]).Name
  • You don't even need an instance of T.... typeof(T) works fine with no instance... Yours will give a different behavior if a subclass is passed into the method (as an argument)..
  • The problem with that code is that if T does not have a parameterless constructor then it will not work.
  • @Nathan - it was just an example to show getting an instance of T. Presumably on a generic method he'll have some T type available. @Reed - you're correct of course, I assumed that was what he was after.
  • Another issue would be in the case that T is an abstract class or an interface - the above code would not work. In the case that there is a generic type constraint (the "where") then this type of code can be safe since we know the constructor and may actually have reasons to instantiate the type. Other than that instantiation is wasteful.