我希望使用的是从外边传过来的类型tt来调用test1范型方法
class Program
    {
        static void Main(string[] args)
        {
            MyClass m = new MyClass();
            m.tt = typeof(Program);
            m.test2();
        }
            }
    [Serializable]
    class MyClass
    {
        public Type tt { get; set; }
        public int userid { get; set; }
        public string Name { get; set; }
        public string test2()
        {
           // test1<T>();
           我希望使用的是从外边传过来的类型tt来调用test1范型方法
        }
        public string test1<T>()
        {
            return typeof(T).ToString();
        }
        
    }

解决方案 »

  1.   

        using System;    class Program 
        { 
            static void Main(string[] args) 
            { 
                MyClass m = new MyClass(); 
                m.tt = typeof(Program); 
                string s = m.test2(); 
                Console.WriteLine(s);
            } 
                } 
        [Serializable] 
        class MyClass 
        { 
            public Type tt;        public string test2() 
            { 
              return test1(tt); 
            }         public string test1 <T>(T x) 
            { 
                return x.GetType().ToString(); 
            } 
            
        }
      

  2.   

        using System;    class Program 
        { 
            static void Main(string[] args) 
            { 
                MyClass m = new MyClass(); 
                m.tt = typeof(Program); 
                string s = m.test2(); 
                Console.WriteLine(s);
            } 
        }     [Serializable] 
        class MyClass 
        { 
            public Type tt;        public string test2() 
            { 
              return test1(tt); 
            }         public string test1 <T>(T x) 
            { 
                return x.ToString(); 
            } 
        }
      

  3.   

    主要是test1这个方法是写好的格式就格式就是这个样子了,不能改成楼上的那种
      

  4.   


    MyClass instance = ...
    Type genericParameter = ...
    object[] parameters = ...
    object result = typeof(MyClass).GetMethod("test1").MakeGenericMethod(genericParameter).Invoke(instance, parameters);