views:

1039

answers:

2

Hello,

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?

+13  A: 

Use Assembly.GetTypes. For example:

Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}
Jon Skeet
+8  A: 

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
How could I reference a completely different assembly that is in my solution?
Alex
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