您当前的位置: 首页 >  c#

苍狼王unity学院

暂无认证

  • 2浏览

    0关注

    305博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#通过反射调用程序集DLL类中的方法_补充第3篇

苍狼王unity学院 发布时间:2019-07-02 10:41:42 ,浏览量:2

1、直接上代码 using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace _11调用方法 { class Program { static void Main(string[] args) { Type stu = typeof(Student); // 1、string //MethodInfo meth = stu.GetMethod(“Say”, new Type[] { typeof(string) }); // object obj = Activator.CreateInstance(stu); // meth.Invoke(obj, new object[] { “你好啊” }); // Console.ReadKey();

        // 2、int
        //**MethodInfo** meth = stu.**GetMethod**("Say", new Type[] { typeof(int) });
        //object obj = **Activator**.CreateInstance(stu);
        //meth.Invoke(obj, new object[] { 23 });
        //Console.ReadKey();


        // 3、无参
        MethodInfo meth = stu.GetMethod("Say", new Type[] { });
        object obj = Activator.CreateInstance(stu);
        meth.Invoke(obj, null);
        Console.ReadKey();
    }

    public class Student
    {
        public void Say(string str)
        {
            Console.WriteLine("++++++++++++++ " + str);
        }
        public void Say(int num)
        {
            Console.WriteLine(num);
        }
        public void Say()
        {
            Console.WriteLine("------------------- ");
        }
    }
}

}

2、附上结果图 在这里插入图片描述在这里插入图片描述 在这里插入图片描述

关注
打赏
1665389160
查看更多评论
立即登录/注册

微信扫码登录

0.0377s