How to get the typeof(T) from a generic method with base type constraint?

前端 未结 2 469
醉话见心
醉话见心 2021-01-22 00:40

I have the following spec to help illustrate the problem:

class when_getting_type_of_generic_argument_using_subtype_instance
{
    static GenericTypeTester _gene         


        
2条回答
  •  孤街浪徒
    2021-01-22 00:49

    Just add the constrait "class" to your method :

     class GenericTypeTester
     {
       public Type Test(T dog) where T : IPet, class
       {
         return typeof (T);
       }
     }
    

提交回复
热议问题