Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a list of all loaded Types in C#?

I need to retrieve all enums that were loaded from a given set of Assemblies.

like image 447
rui Avatar asked Dec 16 '09 11:12

rui


People also ask

How do you get type objects from assemblies that are already loaded?

Use Type. GetType to get the Type objects from an assembly that is already loaded.

Which of the given methods is used in C# to get the details about information types in an assembly during runtime?

Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace. The System.

Can you have a list in a list C#?

List, nested. A List can have elements of List type. This is a jagged list, similar in syntax to a jagged array. Lists are not by default multidimensional in C#.


2 Answers

Here is a more functional solution:

AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(t => t.IsEnum)
like image 80
Brad Avatar answered Sep 22 '22 16:09

Brad


List<Type> list = new List<Type>();
foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
{
    foreach (Type t in ass.GetExportedTypes())
    {
        if (t.IsEnum)
        {
            list.Add(t);
        }
    }
}

That should do, for all assemblies loaded by the current Appdomain, to get just from defined assemblies, just adjust ;-)

like image 33
Oliver Friedrich Avatar answered Sep 22 '22 16:09

Oliver Friedrich