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

后端 未结 3 800
半阙折子戏
半阙折子戏 2020-12-10 00:03

If I have a method signature like

public string myMethod( ... )

How can I, inside the method, get the name of the type that was g

3条回答
  •  爱一瞬间的悲伤
    2020-12-10 00:45

    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()
        {
            return typeof(T).FullName;
        }
    
        static void Main(string[] args)
        {
            Console.WriteLine(MyMethod());
    
            Console.ReadKey();
        }
    
    }
    

    Running the above prints (as expected):

    System.Int32
    

提交回复
热议问题