Invoke generic method

 abstract class A
    { }

    class A1 : A { }
    class A2 : A { }

    abstract class B { }
    class B1 : B { }
    class B2 : B { }
    class TestClass
    {
        void TestMethod<T>() where T : A
        {
            Type val = null;
            if (typeof(T) == typeof(A1))
            {
                val = typeof(B1);
            }
            else if (typeof(T) == typeof(A2))
            {
                val = typeof(B2);
            }

            var mi = this.GetType().GetMethod("TestMethod2", System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance);
           

            var fooRef = mi.MakeGenericMethod(val);

            count = 456;
            fooRef.Invoke(this, null);

        }
        int count = 123;
          public void TestMethod2<T>() where T : B
        {
            var g = typeof(T);
            Console.WriteLine(count);
        }
        public TestClass()
        {
            TestMethod<A1>();
        }
    }

留言

這個網誌中的熱門文章

msdeploy include ApplicationPool setting and ignore recycle time