How to get the names of all classes wich are derived from a base class ?
-
Hi does anyone know how i can get the names of all available classes wich are derived from a base class ?
Here's how to get all derived classes of a base class in one assembly:
List<Type> derived = new List<Type>();
Type baseClassType = typeof(BaseClass);
foreach(Type t in baseClassType.Assembly.GetTypes())
{
if(t.IsSubclassOf(baseClassType))
{
derived.Add(t);
}
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi does anyone know how i can get the names of all available classes wich are derived from a base class ?
With this code you can get all derived classes from the current assembly and its referenced assemblies... (this should be all assemblies in the project)
List<Type> derives = new List<Type>(); foreach( AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies() ) { Assembly assembly = Assembly.Load( name ); foreach( Type class in assembly.GetTypes() ) { if( class.IsSubclassOf( typeof( BaseClass ) ) ) { derives.Add( class ); } } }
-
With this code you can get all derived classes from the current assembly and its referenced assemblies... (this should be all assemblies in the project)
List<Type> derives = new List<Type>(); foreach( AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies() ) { Assembly assembly = Assembly.Load( name ); foreach( Type class in assembly.GetTypes() ) { if( class.IsSubclassOf( typeof( BaseClass ) ) ) { derives.Add( class ); } } }
It will be all referenced assemblies, but not necessarily all assemblies in the project. Referenced assemblies are those that are actually listed in the References tree in Visual Studio and imported by namespace into the project. If you have any late bound assemblies (assemblies loaded by reflection using
Assembly.Load()
), they won't be in this list.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]