123

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?

Alex
  • 75,813
  • 86
  • 255
  • 348
  • If your intention is to examine an assembly that is not referenced by your project, see my updated answer. – Thorarin Aug 22 '09 at 10:35

2 Answers2

185

Use Assembly.GetTypes. For example:

Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    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. – dyslexicanaboko Mar 28 '14 at 18:24
  • 1
    @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?) – Jon Skeet Mar 28 '14 at 19:24
  • 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. – dyslexicanaboko Mar 28 '14 at 19:37
  • @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? – Jon Skeet Mar 28 '14 at 19:39
  • 1
    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. – dyslexicanaboko Mar 28 '14 at 19:52
  • Doesn't this also include interfaces? – Ray Aug 24 '14 at 16:41
  • 1
    @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. – Jon Skeet Aug 24 '14 at 18:19
  • for .NET Core it should be typeof(string).GetTypeInfo().Assembly – Andrzej Martyna Nov 17 '17 at 14:49
  • 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)` – Byron Jones Jun 08 '18 at 02:37
114

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.

Thorarin
  • 47,289
  • 11
  • 75
  • 111
  • How could I reference a completely different assembly that is in my solution? – Alex Aug 22 '09 at 10:13
  • 14
    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. – Jon Skeet Aug 22 '09 at 10:17
  • 2
    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");` – Kash Mar 08 '12 at 18:36
  • 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? – Er Mayank Aug 28 '14 at 20:37
  • @JonSkeet How would this be achievable in a "modern" c# environment? Such as when doing UWP development. UWP doesn't have the GetExecutingAssembly() method. – Daniel Armstrong Oct 27 '16 at 05:42
  • @DanielArmstrong: See my comment above... if you know any type in the assembly, just obtain the assembly from that. – Jon Skeet Oct 27 '16 at 05:44
  • all assemblies within solution can be access from here AppDomain.CurrentDomain.GetAssemblies() – mrosiak Oct 24 '17 at 16:06