流浪のwolf

卷帝

导航

动态去读 dll 文件

// 反射动态读取 dll
// Assembly assembly = Assembly.LoadFile(); 路径
// Assembly assembly = Assembly.LoadFrom(); // 全名称
// Assembly assembly = Assembly.Load(); // dll 名称不用加上后缀

 

 

  // 反射动态读取 dll
  // Assembly assembly = Assembly.LoadFile();  路径
  var path = @"C:\Users\朱龙旭\Desktop\WebApplication1\WebApplication1\bin\Debug\net6.0\WebApplication1.dll";
  Assembly assembly = Assembly.LoadFile(path);  // 全名称
  // Assembly assembly = Assembly.Load();  // dll 名称不用加上后缀
  foreach(Type type in assembly.GetTypes())
  {
      // Console.WriteLine("type类型", type);
      // Console.WriteLine(type.FullName);
      Console.WriteLine("================方法===============");
      // 方法
      foreach (var method in type.GetMethods())
      {
          // Console.WriteLine(method);
      }
      Console.WriteLine("================字段===============");
      // 字段
      foreach (var filed in type.GetFields())
      {
          // Console.WriteLine(filed);
      }
  }
  // 获取某个具体的类型  参数是类的全名称
  Type type1 = assembly.GetType("WebApplication1.Controllers.SystemApi.UserController");
  Console.WriteLine("获取某个具体的类型(类)通过命名空间+类名");
  Console.WriteLine(type1);

  // 获取到了类 然后通过type 创建对象
  object? o = Activator.CreateInstance(type1);

  // 总结  通过反射 dll 可以获取项目的所有方法和字段属性等

 

posted on 2024-04-01 19:31  流浪のwolf  阅读(3)  评论(0编辑  收藏  举报