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、附上结果图
