Как я получаю список всех загруженных Типов в C#?

Я должен получить все перечисления, которые были загружены из данного набора блоков.

10
задан rui 16 December 2009 в 11:26
поделиться

4 ответа

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 ;-)

15
ответ дан 3 December 2019 в 16:10
поделиться

Предполагая, что у вас есть список объектов Assembly , которые вы хотите проверить:

IEnumerable<Assembly> assemblies; // assign the assemblies you want to check here

foreach (Assembly a in assemblies) {
    foreach (Type t in assembly.GetTypes()) {
        if (t.IsEnum) {
            // found an enum! Do whatever...
        }
    }
}
3
ответ дан 3 December 2019 в 16:10
поделиться

You should be able to use Assembly.GetTypes() to get all the types for the assembly. For each type, you can use Type.IsEnum property to see if it's an enum.

2
ответ дан 3 December 2019 в 16:10
поделиться

You can also use LINQ to return a list of all enum types from a list of assemblies.

IEnumerable<Assembly> assemblies;
// give assemblies some value
var enums = from assembly in assemblies let types = assembly.GetTypes() from type in types where type.IsEnum select type;

enums will be of type IEnumerable.

2
ответ дан 3 December 2019 в 16:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: