Get All Derived Types of a Type

Get a List Type of all derived types in a list of objects that inherit from an abstract class

There is no language feature to help you with this. The simplest way would be to use LINQ, first use Select to get all the types and use Distinct

parts.Select(x => x.GetType()).Distinct();

Find all derived types of generic class


var result = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.BaseType != null && t.BaseType.IsGenericType &&
t.BaseType.GetGenericTypeDefinition() == typeof(GenericClass<>));

C# How do I get all the fields of a specific type from a derived class within the base class?


public abstract class NetBehaviour
{
void setSyncFloat (SyncFloat[] values)
{
// Find all SyncFloat fields in the child
// class (the one that derived this class).
var fields = GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(fi => fi.FieldType == typeof(SyncFloat));
}
}

BindingFlags have been included because I assume you want private fields.

Note that GetType() will get the runtime type i.e. the subclass type.

Discovering derived types using reflection

pretty much the same as Darin's but here you go..

    public static List<Type> FindAllDerivedTypes<T>()
{
return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));
}

public static List<Type> FindAllDerivedTypes<T>(Assembly assembly)
{
var derivedType = typeof(T);
return assembly
.GetTypes()
.Where(t =>
t != derivedType &&
derivedType.IsAssignableFrom(t)
).ToList();

}

used like:

var output = FindAllDerivedTypes<System.IO.Stream>();
foreach (var type in output)
{
Console.WriteLine(type.Name);
}

outputs:

NullStream
SyncStream
__ConsoleStream
BufferedStream
FileStream
MemoryStream
UnmanagedMemoryStream
PinnedBufferMemoryStream
UnmanagedMemoryStreamWrapper
IsolatedStorageFileStream
CryptoStream
TailStream

How to Get List of Simple Inherited Types?

You're looking for an extension method

namespace ExtensionMethods
{
public static class MyExtensions
{
public static IEnumerable<Type> GetInheritedTypes(this Type BaseClass)
{

IEnumerable<Type> subclassTypes = Assembly
.GetAssembly(BaseClass)
.GetTypes()
.Where(type => type.IsSubclassOf(BaseClass));
return subclassTypes;
}
}
}

Don't forget you need some reflection here (using System.Reflection;)

And then you can use it like you want:
-include the extension method where you need it (using ExtensionMethods;)
and invoke it:

var listofChildTypes = typeof(Animal).GetInheritedTypes();

Get all inherited classes of an abstract class

This is such a common problem, especially in GUI applications, that I'm surprised there isn't a BCL class to do this out of the box. Here's how I do it.

public static class ReflectiveEnumerator
{
static ReflectiveEnumerator() { }

public static IEnumerable<T> GetEnumerableOfType<T>(params object[] constructorArgs) where T : class, IComparable<T>
{
List<T> objects = new List<T>();
foreach (Type type in
Assembly.GetAssembly(typeof(T)).GetTypes()
.Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
{
objects.Add((T)Activator.CreateInstance(type, constructorArgs));
}
objects.Sort();
return objects;
}
}

A few notes:

  • Don't worry about the "cost" of this operation - you're only going to be doing it once (hopefully) and even then it's not as slow as you'd think.
  • You need to use Assembly.GetAssembly(typeof(T)) because your base class might be in a different assembly.
  • You need to use the criteria type.IsClass and !type.IsAbstract because it'll throw an exception if you try to instantiate an interface or abstract class.
  • I like forcing the enumerated classes to implement IComparable so that they can be sorted.
  • Your child classes must have identical constructor signatures, otherwise it'll throw an exception. This typically isn't a problem for me.

Find a derived class in a list of its base class

If you pass in an instance of DerivedClassB, you can find all instances of DerivedClassB by comparing the actual type of the instance passed in and of the instances in the list:

public IEnumerable<BaseClass> FindClass (BaseClass @class){ 
return myList.Where(c => c.GetType() == @class.GetType());
}

How to find all the types in an Assembly that Inherit from a Specific Type C#

Something like:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t));
}

If you need to handle generics, that gets somewhat trickier (e.g. passing in the open List<> type but expecting to get back a type which derived from List<int>). Otherwise it's simple though :)

If you want to exclude the type itself, you can do so easily enough:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
return assembly.GetTypes().Where(t => t != baseType &&
baseType.IsAssignableFrom(t));
}

Note that this will also allow you to specify an interface and find all the types which implement it, rather than just working with classes as Type.IsSubclassOf does.



Related Topics



Leave a reply



Submit