0

How can i get the System.Type of a (Type in a generic function)? I need to get a string representation of the class name.

typeof(T).GetType().Name

gives me 'runtimetype', which i do not want.

EClaesson
  • 1,568
  • 2
  • 15
  • 26

2 Answers2

4

Just do:

typeof(T)

That will result in the actual type of T whatever T is for that particular instance of your generic class/method. If T happened to be System.String, you would get an instance of the type representing System.String.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Elaborating so you understand what is happening: `typeof(T)` is a `System.Type` that describes `T`. Therefore, `typeof(T).GetType()` is a `System.Type` that describes `System.Type`. The name is therefore `System.Type`. – Raymond Chen Aug 30 '12 at 04:51
0

Can you use like this ?

Type thisType = this.GetType();   // Get your current class type
thisType.FullName                 // Get your Assembly Name.

Edit in you class,

Type thistype = TypeOf(T);
thisType.Name                    // Get you class name 
Turbot
  • 5,095
  • 1
  • 22
  • 30
  • No i can not. My function looks something like this: public static T DoSomething() { string className = ???; DoSomethingWithClassName(); } – EClaesson Aug 30 '12 at 03:16
  • 1
    `TypeOf` does not exist in C#, it is the [`typeof`](http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.110).aspx) operator. – Christian.K Aug 30 '12 at 04:13