Created
December 28, 2024 23:46
-
-
Save karenpayneoregon/112c4e5027f8114abc17cd931307591f to your computer and use it in GitHub Desktop.
Interface helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helpers | |
{ | |
/// <summary> | |
/// Retrieves the names of all entities that implement a specified interface type. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// The interface type to search for. Must be a class type and an interface. | |
/// </typeparam> | |
/// <returns> | |
/// A list of strings representing the names of all entities that implement the specified interface. | |
/// </returns> | |
/// <exception cref="ArgumentException"> | |
/// Thrown when the specified type <typeparamref name="T"/> is not an interface. | |
/// </exception> | |
public static List<string> GetAllEntityNames<T>() where T : class | |
{ | |
if (!typeof(T).IsInterface) | |
throw new ArgumentException("T must be an interface."); | |
return Enumerable | |
.Select<Type, string>(GetAllEntities<T>(), x => x.Name) | |
.ToList(); | |
} | |
/// <summary> | |
/// Retrieves all types that implement a specified interface type. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// The interface type to search for. Must be a class type and an interface. | |
/// </typeparam> | |
/// <returns> | |
/// A list of <see cref="Type"/> objects representing all types that implement the specified interface. | |
/// </returns> | |
/// <exception cref="ArgumentException"> | |
/// Thrown when the specified type <typeparamref name="T"/> is not an interface. | |
/// </exception> | |
public static List<Type> GetAllEntities<T>() where T : class | |
{ | |
if (!typeof(T).IsInterface) | |
throw new ArgumentException("T must be an interface."); | |
return AppDomain.CurrentDomain.GetAssemblies() | |
.SelectMany(x => x.GetTypes()) | |
.Where(x => typeof(T).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract) | |
.ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment