C#: List All Classes in Assembly
Asked Answered
C

2

126

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?

Centuple answered 22/8, 2009 at 9:59 Comment(1)
If your intention is to examine an assembly that is not referenced by your project, see my updated answer.Evalynevan
Z
189

Use Assembly.GetTypes. For example:

Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}
Zincography answered 22/8, 2009 at 10:1 Comment(9)
Any suggestions for large assemblies? When I run this code for a 13.8 MB assembly my VS instance hangs for what feels like indefinitely. I tried a small 9 KB assembly and it worked just fine. I know what you are thinking - why do you have a 13.8 MB assembly - it is part of my data layer generated using a NetTeirs template. We have many tables.Cirone
@dyslexicanaboko: Well if you have lots of types, it will take a long time to enumerate them all. How many types are in your assembly though? And what are you doing with them? (Are you sure the problem is in extracting the types, or just what you're doing afterwards?)Zincography
I can't get past the asm.GetTypes() call, it just hangs - I mean it is obviously the fact that there are a lot of Types - I can't do anything because it is drilling away at trying to get them all. My CPU shoots to 30% on one of my 4 cores. I mean really I am just wondering if there is a way to say, "Hey - only look in THIS namespace" - I am under the impression that it's not possible because the GetTypes() method only has an empty constructor. I am trying to make an object browser of sorts.Cirone
@dyslexicanaboko: I'm afraid I don't know of any more efficient way of finding the types. Can you open it in Reflector or something similar?Zincography
Good point, I haven't tried that yet. My work around for now is to just target the classes directly using asm.GetType(fullyQualifiedClassName) - that works, but showing a list of classes to the user is not possible, which is what I wanted. This isn't client facing btw, I am using it internally for myself and other developers.Cirone
Doesn't this also include interfaces?Builtin
@DebugErr: Yes - it's easy enough to make it just classes if you want, but my guess is that the OP probably really wanted all the types.Zincography
for .NET Core it should be typeof(string).GetTypeInfo().AssemblyWarty
Since OP expressed a desire to list all classes in an assembly, it's worth noting that 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
E
115

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.

Evalynevan answered 22/8, 2009 at 10:11 Comment(7)
How could I reference a completely different assembly that is in my solution?Centuple
The easiest way is to use typeof with a type you know is in that assembly, and then the Assembly property, as in my example.Zincography
If you want to reference an assembly, say abc.dll, that is in your solution and if you are ok hardcoding the dll name, another approach to referencing the assembly is: ` Assembly assembly = Assembly.Load("abc");`Admeasure
It loads only current assembly. I have a application or exe using 4 dlls or projects. How can i get names of the classes of those dlls?Claver
@JonSkeet How would this be achievable in a "modern" c# environment? Such as when doing UWP development. UWP doesn't have the GetExecutingAssembly() method.Ladonna
@DanielArmstrong: See my comment above... if you know any type in the assembly, just obtain the assembly from that.Zincography
all assemblies within solution can be access from here AppDomain.CurrentDomain.GetAssemblies()Brendis

© 2022 - 2024 — McMap. All rights reserved.