0

I Have a Solution with 2 Proyects:

  • __mvc4
  • 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

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

enter image description here

    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!

Devolus
  • 21,661
  • 13
  • 66
  • 113
rr7
  • 163
  • 2
  • 12
  • What are you trying to do that reflecting the assembly is the solution? This awfully looks like a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), could you explain what your end goal that getting all of the classes and methods would help you solve? – Scott Chamberlain Jan 08 '14 at 21:17
  • "classes" in C# are called "Types" in .Net - so method you are looking is [Assembly.GetTypes](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes%28v=vs.110%29.aspx). Fortunately for methods C# and .Net agree on name - `GetMethods` of each type. – Alexei Levenkov Jan 08 '14 at 21:17
  • sounds as it was answered before http://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection – T.S. Jan 08 '14 at 21:21
  • My end goal is to get all the Methods from all the classes that are in another project in the same solution @ScottChamberlain – rr7 Jan 08 '14 at 22:17
  • the link was really helpful , thanks @T.S. – rr7 Jan 08 '14 at 22:20

2 Answers2

3

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

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
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.

poy
  • 10,063
  • 9
  • 49
  • 74