从另一个程序集中获取类和方法

时间:2022-11-19 10:41:41

I Have a Solution with 2 Proyects:

我有2个Proyects的解决方案:

  • __mvc4
  • __mvc4
  • getClassesMethods
  • getClassesMethods

the getClassesMethods have a reference to __mvc4 , so I want to get All the Classes and Methods from a namespace inside the __mvc4 Project so I have this code to Load and get all the Assemblies from the AppDomain

getClassesMethods引用了__mvc4,所以我想从__mvc4项目中的命名空间中获取所有类和方法,所以我有这个代码加载并从AppDomain获取所有程序集

Here's is the breakpoint at the var asssemblies (contains the __mvc4 Assembly)

这是var asssemblies的断点(包含__mvc4程序集)

从另一个程序集中获取类和方法

    namespace getClassesMethods
    {
        class Program
        {
            static void Main(string[] args)
            {


                var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
                var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();

                var referencedPaths = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
                var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
                toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));

                var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();

But I don't know how to get all the Classes and Methods from the ___mvc4 Assembly in the var assemblies. Hope you can help me!

但我不知道如何从var程序集中的___mvc4程序集中获取所有类和方法。希望你能帮我!

2 个解决方案

#1


2  

Just a sample

只是一个样本

foreach(var asm in loadedAssemblies)
{
   var classes = asm.GetTypes(); // you can use GetTypes to get all classes in that assembly
   foreach(var c in classes)
   {
      // you can get all methods which is defined in this class with GetMethods
      var methods = c.GetMethods();

      // or you can get all properties defined in this class
      var props = c.GetProperties();
   }
}  

For more information see Type class and read this documentation

有关更多信息,请参阅键入类并阅读本文档

#2


1  

Try:

尝试:

var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm=>asm.GetTypes());
var methods = types.SelectMany(type=>type.GetMethods());

NOTE

注意

"classes" are called "types" in .NET.

“类”在.NET中称为“类型”。

#1


2  

Just a sample

只是一个样本

foreach(var asm in loadedAssemblies)
{
   var classes = asm.GetTypes(); // you can use GetTypes to get all classes in that assembly
   foreach(var c in classes)
   {
      // you can get all methods which is defined in this class with GetMethods
      var methods = c.GetMethods();

      // or you can get all properties defined in this class
      var props = c.GetProperties();
   }
}  

For more information see Type class and read this documentation

有关更多信息,请参阅键入类并阅读本文档

#2


1  

Try:

尝试:

var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm=>asm.GetTypes());
var methods = types.SelectMany(type=>type.GetMethods());

NOTE

注意

"classes" are called "types" in .NET.

“类”在.NET中称为“类型”。