//实例方法1 //new CreateCalss cc1 = new CreateCalss(); //实例方法2 //获取当前程序集 Assembly asse = Assembly.GetExecutingAssembly(); //获取程序集对象Type t查看方法和字段 Type t = asse.GetType("sp." + "CreateCalss"); //创建对象类型 CreateCalss cc2 = (CreateCalss)Activator.CreateInstance(t); //实例方法3 //加载指定程序集并创建对象实例 CreateCalss cc3 = (CreateCalss)Assembly.Load("sp").CreateInstance("sp.CreateCalss"); //假设需求根据用户的选择创建一个实例 这个时候需要动态创建对象 //比如选择A A a=new A(); //比如选择B B b=new B(); Assembly ass = Assembly.GetExecutingAssembly(); Type tp = ass.GetType("sp."+"CreateCalss2"); object o = Activator.CreateInstance(tp); Isp isp = o as Isp; isp.Open();
public class CreateCalss : Isp { public int i; public void Open() { Console.WriteLine("1"); } } public class CreateCalss2 : Isp { public int i; public void Open() { Console.WriteLine("2"); } } interface Isp { void Open(); }
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Type type = typeof(ReflectTest); object reflectTest = Activator.CreateInstance(type); //不带参数且不返回值的方法的调用 MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn"); methodInfo.Invoke(reflectTest, null); ReflectTest r = (ReflectTest)Activator.CreateInstance(type); r.MethodWithNoParaNoReturn(); Console.WriteLine(); ////不带参数且有返回值的方法的调用 //methodInfo = type.GetMethod("MethodWithNoPara"); //Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString()); //Console.WriteLine(); ////带参数且有返回值的方法的调用 //methodInfo = type.GetMethod("Method1", new Type[] { typeof(string) }); //Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试" }).ToString()); //Console.WriteLine(); ////带多个参数且有返回值的方法的调用 //methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) }); //Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", 100 }).ToString()); ////Console.WriteLine(); ////methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) }); ////string outStr = ""; ////Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", outStr }).ToString()); //Console.WriteLine(); ////静态方法的调用 //methodInfo = type.GetMethod("StaticMethod"); //Console.WriteLine(methodInfo.Invoke(null, null).ToString()); Console.ReadKey(); } } public class ReflectTest { public void MethodWithNoParaNoReturn() { Console.WriteLine("不带参数且不返回值的方法"); } public string MethodWithNoPara() { Console.WriteLine("不带参数且有返回值的方法"); return "MethodWithNoPara"; } public string Method1(string str) { Console.WriteLine("带参数且有返回值的方法"); return str; } public string Method2(string str, int index) { Console.WriteLine("带参数且有返回值的方法"); return str + index.ToString(); } public string Method3(string str, out string outStr) { outStr = "bbbb"; Console.WriteLine("带参数且有返回值的方法"); return str; } public static string StaticMethod() { Console.WriteLine("静态方法"); return "cccc"; } } }
反射:
class clsDemo { public void csDemo(string str) { MessageBox.Show(str); } }
System.Type t = typeof(Person); var obj = Activator.CreateInstance(t, null); t.InvokeMember("csDemo", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { "hell world!" });
使用 :dynamic dynamic obj= Activator.CreateInstance(t, null); obj.csDemo("hell world!");