标签:
根据程序集的信息,动态的创建类并执行方法
using System; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; namespace ConsoleApplication1 { internal class Program { public class Test { public void Show() { Console.WriteLine("zk"); } } private static void Main(string[] args) { //获得当前执行模块 Assembly assem = Assembly.GetExecutingAssembly(); Console.WriteLine(assem.FullName); Type[] tps = assem.GetTypes(); foreach (Type tp in tps) { Console.WriteLine(tp.FullName); } Type t = assem.GetType("ConsoleApplication1.Program+Test"); //Console.WriteLine(t.FullName); //创建实例 object o = Activator.CreateInstance(t); //直接调用 ((Test) o).Show(); //通过反射方法调用 MethodInfo mt = t.GetMethod("Show"); mt.Invoke(o, new object[] { }); } } }
标签:
原文地址:http://www.cnblogs.com/zkzk945/p/5134470.html