2

I tried Assembly.ReflectionOnlyLoadFrom(@"path\System.Core.dll") and ReflectionOnlyLoad but i got exceptions and errors. How do i properly get all the namespaces/classes in an assembly?

For example i got this exception.

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

3 Answers3

3

To load an assembly and then get a list of all types:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");
Type[] types = assembly.GetTypes();

Unfortunately this will throw an exception if any of the types exposed cannot be loaded, and sometimes this load failure cannot be avoided. In this case however the thrown exception contains a list of all types that were successfully loaded and so we can just do this:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");

Type[] types;
try
{
    types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
    types = ex.Types;
}

This will give you a list of all types, including interfaces, structs, enums etc... (If you want just the classes then you can filter that list).

Justin
  • 84,773
  • 49
  • 224
  • 367
2

If you can reference System.Core then

    List<string> namespaces = new List<string>();

    var refs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

    foreach (var rf in refs) {
        if (rf.Name == "System.Core")
        {
            var ass = Assembly.Load(rf);
            foreach (var tp in ass.GetTypes())
            {
                if (!namespaces.Contains(tp.Namespace))
                {
                    namespaces.Add(tp.Namespace);
                    Console.WriteLine(tp.Namespace);
                }
            }
        }
    }

If you cannot, you will need to attach to the AssemblyResolve event of the CurrentDomain and load all assemblies of types that System.Core.dll uses when loading the dll.

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
1

Here is your answer to your question. I do not need to copy & paste it here for you, it might be greener to save space rather that copying code from the other thread. :-)

Community
  • 1
  • 1
Peyton Crow
  • 872
  • 4
  • 9
  • Have you read the thread? Try to scroll down and you will see your answer there. – Peyton Crow Apr 29 '11 at 03:49
  • The answer that looks like it may work has `CurrentDomain_ReflectionOnlyAssemblyResolve` which i cant figure out and its taking a bit to google but Marino has already showed me the solution (which is different from all those answers). None of those answers use GetReferencedAssemblies. Also i found `LoadFrom` works which also none of those answer has.... –  Apr 29 '11 at 03:52
  • Ah yes I did not look because the question did not mention it... I'm using the Assembly resolve in one of my loaders: http://stackoverflow.com/questions/5708456/register-c-com-object-without-having-a-real-file-for-regasm but assembly resolve seemed a bit more complex than needed for this case. It is only needed if you cannot reference what you want to reflect... – Marino Šimić Apr 29 '11 at 03:52
  • Ah yes, then do not mind the answer, you might save space too. – Peyton Crow Apr 29 '11 at 03:53
  • @Marino yes, loading was the problem. None of those answers mention the functions i required. Anyways this Q is done. –  Apr 29 '11 at 03:54