Skip to content

Commit

Permalink
chore: Reflection FindImplementationsOf
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-osipov committed Jan 24, 2021
1 parent 735c090 commit e7b0352
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions Runtime/Utilities/ReflectionUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -34,7 +35,33 @@ public static Type FindType(string typeFullName)
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(type => type.FullName == null || type.FullName.Equals(typeFullName));
}


/// <summary>
/// Find all types that implement `T`.
/// </summary>
/// <typeparam name="T">Base type.</typeparam>
/// <returns>Returns all types that are implement provided base type.</returns>
public static IEnumerable<Type> FindImplementationsOf<T>()
{
var baseType = typeof(T);
return FindImplementationsOf(baseType);
}


/// <summary>
/// Find all types that implement `baseType`.
/// </summary>
/// <param name="baseType">Base type.</param>
/// <returns>Returns all types that are implement provided base type.</returns>
public static IEnumerable<Type> FindImplementationsOf(Type baseType)
{
var implementations = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => baseType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract);

return implementations;
}

/// <summary>
/// Get property value from an object by it's name.
/// </summary>
Expand All @@ -46,7 +73,7 @@ public static object GetPropertyValue(object src, string propName, BindingFlags
{
return src.GetType().GetProperty(propName, bindingAttr).GetValue(src, null);
}

/// <summary>
/// Get property value from an object by it's name.
/// </summary>
Expand Down

0 comments on commit e7b0352

Please sign in to comment.