AttributeHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace UnityEngine.TestTools
  5. {
  6. internal static class AttributeHelper
  7. {
  8. internal static Type GetTargetClassFromName(string targetClassName, Type attributeInterface)
  9. {
  10. Type targetClass = null;
  11. foreach (var assemblyName in ScriptingRuntime.GetAllUserAssemblies())
  12. {
  13. // we need to pass the assembly name without the .dll extension, so removing that first
  14. var name = Path.GetFileNameWithoutExtension(assemblyName);
  15. targetClass = Type.GetType(targetClassName + "," + name);
  16. if (targetClass != null)
  17. break;
  18. }
  19. if (targetClass == null)
  20. {
  21. Debug.LogWarningFormat("Class type not found: " + targetClassName);
  22. return null;
  23. }
  24. ValidateTargetClass(targetClass, attributeInterface);
  25. return targetClass;
  26. }
  27. private static void ValidateTargetClass(Type targetClass, Type attributeInterface)
  28. {
  29. var constructorInfos = targetClass.GetConstructors();
  30. if (constructorInfos.All(constructor => constructor.GetParameters().Length != 0))
  31. {
  32. Debug.LogWarningFormat("{0} does not implement default constructor", targetClass.Name);
  33. }
  34. if (!attributeInterface.IsAssignableFrom(targetClass))
  35. {
  36. Debug.LogWarningFormat("{0} does not implement {1}", targetClass.Name, attributeInterface.Name);
  37. }
  38. }
  39. }
  40. }