I'd like to output (programmatically - C#) a list of all classes in my assembly.
Any hints or sample code how to do this? Reflection?
I'd like to output (programmatically - C#) a list of all classes in my assembly.
Any hints or sample code how to do this? Reflection?
Use Assembly.GetTypes
. For example:
Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
Console.WriteLine(type.FullName);
}
GetTypes()
will return ALL types - not simply classes, but also interfaces, enums, etc. If you just want classes, you can use a Linq extension method to filter like so: MyAssembly.GetTypes().Where(t => t.IsClass)
–
Judenberg I'd just like to add to Jon's example. To get a reference to your own assembly, you can use:
Assembly myAssembly = Assembly.GetExecutingAssembly();
System.Reflection
namespace.
If you want to examine an assembly that you have no reference to, you can use either of these:
Assembly assembly = Assembly.ReflectionOnlyLoad(fullAssemblyName);
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(fileName);
If you intend to instantiate your type once you've found it:
Assembly assembly = Assembly.Load(fullAssemblyName);
Assembly assembly = Assembly.LoadFrom(fileName);
See the Assembly class documentation for more information.
Once you have the reference to the Assembly
object, you can use assembly.GetTypes()
like Jon already demonstrated.
typeof
with a type you know is in that assembly, and then the Assembly
property, as in my example. –
Zincography © 2022 - 2024 — McMap. All rights reserved.