当你的才华不能撑起你的野心时,就是你该选择学习的时候了!

C# 类的反射实例 (GetType().Invoke().GetMethod().CreateInstance())

namespace IT.UnitTest
{
    public class Demo
    {
        public string Test(string input)
        {
            return input;
        }

        static public string TestStatic(string input)
        {
            return input;
        }
    }
    public class Tests
    {
        [Test]
        public void Test2()
        {
            var t1 = Type.GetType("IT.UnitTest.Demo");
            var obj = Activator.CreateInstance(t1);
            var output = t1.GetMethod("Test").Invoke(obj, new string[] { "input 1" });
            Assert.IsNotEmpty(output.ToString());

            output = t1.GetMethod("TestStatic").Invoke(obj, new string[] { "input 2" });
            Assert.IsNotEmpty(output.ToString());
        }

    }
}

 

posted @ 2023-04-24 17:14  hofmann  阅读(70)  评论(0)    收藏  举报