ReflectiveEnumerator.cs 977 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Reflection;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public static class ReflectiveEnumerator
  8. {
  9. static ReflectiveEnumerator() { }
  10. public static IEnumerable<T> GetEnumerableOfType<T>(params object[] constructorArgs) where T : class, IComparable<T>
  11. {
  12. List<T> objects = new List<T>();
  13. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  14. foreach (var assembly in assemblies)
  15. {
  16. foreach (Type type in assembly.GetTypes()
  17. .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
  18. {
  19. objects.Add((T)Activator.CreateInstance(type, constructorArgs));
  20. }
  21. }
  22. //foreach (var item in objects) Debug.Log(item);
  23. objects.Sort((x,y) => String.Compare(x.ToString(), y.ToString()));
  24. //objects.Sort();
  25. return objects;
  26. }
  27. }