Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

53 wiersze
1.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.UDP.Editor
  4. {
  5. public static class Utils
  6. {
  7. static Dictionary<string, Type> m_TypeCache = new Dictionary<string, Type>();
  8. private static string[] k_WhiteListedAssemblies = {"UnityEditor"};
  9. public static Type FindTypeByName(string name)
  10. {
  11. if (m_TypeCache.ContainsKey(name))
  12. {
  13. return m_TypeCache[name];
  14. }
  15. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  16. foreach (var assembly in assemblies)
  17. {
  18. if (!AllowLookupForAssembly(assembly.FullName))
  19. continue;
  20. try
  21. {
  22. var types = assembly.GetTypes();
  23. foreach (var type in types)
  24. {
  25. if (type.FullName == name)
  26. {
  27. m_TypeCache[type.FullName] = type;
  28. return type;
  29. }
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. throw new InvalidOperationException(string.Format(
  35. "Count not fetch list of types from assembly {0} due to error: {1}", assembly.FullName,
  36. e.Message));
  37. }
  38. }
  39. return null;
  40. }
  41. private static bool AllowLookupForAssembly(string name)
  42. {
  43. return Array.Exists(k_WhiteListedAssemblies, name.StartsWith);
  44. }
  45. }
  46. }