反射(程序集)
1.添加一个类库文件Person
namespace 程序集测试 { public class Person { public int Age { get; set; } public string Name { get; set; } public void SayHello() { Console.WriteLine("哈喽哇!"); } } }
2.添加一个控制台程序
namespace 程序集 { class Program { static void Main(string[] args) { //获得程序里面加载的所有程序集 //Assembly[] asms = // AppDomain.CurrentDomain.GetAssemblies(); //foreach (Assembly asm in asms) //{ // Console.WriteLine(asm.Location); //} //添加测试集测试dll文件 string filename = @"D:\net\学习\if语句\程序集测试\bin\Debug\程序集测试.dll"; //从文件中加载程序集 Assembly asm = Assembly.LoadFile(filename);//没有添加引用但是会动态添加程序集里面的类 Type[] types = asm.GetTypes();//获得程序集中定义的所有类型 foreach (Type type in types) { Console.WriteLine(type); //获得程序集中定义的所有属性 PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { Console.WriteLine(prop); } } Console.ReadKey(); } } }
浙公网安备 33010602011771号