我想弄一泛型方法.类似如下:  public static T Test<T>(string aa) 
        {
            if(T.getTyep()==string)//  假设
            return aa;
            //return default(T);
        }
就是说根据类型返回相应类型型的值,比如说我调用 的时候传T为int型的,则返回int型的.就是想用泛型来返回不同类型的值.怎么判断T的类型呢.及相应的返回Test<string>("aa");

解决方案 »

  1.   

    这个意思?public static T Test<T>(T aa)
    {
        if (aa != null)
        {
            return aa;
        }
        else
        {
            return default(T);
        }
    }
      

  2.   

    public class m<T,S>
    {
        public  T i ;
        public S str ;
        public m(T I,S Str)
      {
          this.i = I;
          this.str = Str;
      }
    }   m<int,string> mmm=new m<int,string>(120,"dasdasd");
            this.Response.Write(mmm.i.ToString() + "||" + mmm.str);
      

  3.   

    public static T Test<T>(string aa) 
    {
         if (typeof(T)==typeof(int))//  假设
            return int.Parse(aa);
         else if (typeof(T)==typeof(double))
           return double.Parse(aa);
         ...
    }
    我想楼主是想这样吧
      

  4.   

    你在调用的时候就定义了类型了
    返回的时候肯定就是那个类型了
    用typeof就知道啊