反射获取dll文件中的信息:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace GetInfoByDll { class Program { static void Main(string[] args) { string path = @"D:RuPengRupengClassLibraryTestinDebugClassLibraryTest.dll"; Assembly ass = Assembly.LoadFile(path); //Type tper = ass.GetType("ClassLibraryTest.Students"); Type[] tps1 = ass.GetExportedTypes();//获取公共的 Type[] tps = ass.GetTypes(); for (int i = 0; i < tps.Length; i++) { Console.WriteLine(tps[i].Name); } Type typer = ass.GetType("ClassLibraryTest.Persons");//获取实例类型 Object obj = Activator.CreateInstance(typer);//创建实例对象 MethodInfo mt = typer.GetMethod("Say",System.Type.EmptyTypes);//获取无参的方法 mt.Invoke(obj,null);//调用无参的方法 MethodInfo mt2 = typer.GetMethod("SayHello",new Type[]{typeof(string)});//获取有参 的方法 mt2.Invoke(obj, new object[]{"kakaxi"});//调用的方法 Console.ReadKey(); PropertyInfo[] ps = typer.GetProperties();//获取属性 for (int i = 0; i < ps.Length; i++) { if(ps[i].PropertyType.ToString()=="System.String") { ps[i].SetValue(obj,"kakaxi",null);//设置属性值,之前要创建实例对象 } if (ps[i].PropertyType.ToString() == "System.Int32") { ps[i].SetValue(obj, 100, null); } Console.WriteLine(ps[i].Name); Console.WriteLine(ps[i].GetValue(obj,null));//获取实例属性的值 } Console.ReadKey(); } } }