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