反射的总结

来源:互联网 发布:sqlserver trigger 编辑:程序博客网 时间:2024/06/06 20:50

1.反射的可扩展性,可以把要加载的dll文件放在App.config中

<?xml version="1.0" encoding="utf-8" ?><configuration>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />  </startup>  <appSettings>    <add key="ConsoleApplication1" value="ConsoleApplication1.dll"/>  </appSettings></configuration>

2.使用反射创建对象并调用方法

        private void button1_Click(object sender, EventArgs e)        {            var assembly = Assembly.Load("ConsoleApplication1");            Type[] trye = assembly.GetTypes();            object obj = Activator.CreateInstance(trye[0]);            Console.WriteLine("----------GetModules--------------");            foreach (var item in assembly.GetModules())            {                Console.WriteLine(item.Name);            }            Console.WriteLine("----------GetMethods--------------");            foreach (var item in trye[0].GetMethods())            {                Console.WriteLine(item.Name);                //item.Invoke(obj, null);            }            MethodInfo methodinfo = trye[0].GetMethod("SayGoodmorning");            methodinfo.Invoke(obj, null);        }


3.使用反射找重载的方法 public MethodInfo GetMethod(string name, Type[] types);


原创粉丝点击