Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RuntimeUtil.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEngine.Assertions;
  8. namespace XCharts.Runtime
  9. {
  10. public static class RuntimeUtil
  11. {
  12. public static bool HasSubclass(Type type)
  13. {
  14. var typeMap = GetAllTypesDerivedFrom(type);
  15. foreach (var t in typeMap)
  16. {
  17. return true;
  18. }
  19. return false;
  20. }
  21. public static IEnumerable<Type> GetAllTypesDerivedFrom<T>()
  22. {
  23. #if UNITY_EDITOR && UNITY_2019_2_OR_NEWER
  24. return UnityEditor.TypeCache.GetTypesDerivedFrom<T>();
  25. #else
  26. return GetAllAssemblyTypes().Where(t => t.IsSubclassOf(typeof(T)));
  27. #endif
  28. }
  29. public static IEnumerable<Type> GetAllTypesDerivedFrom(Type type)
  30. {
  31. #if UNITY_EDITOR && UNITY_2019_2_OR_NEWER
  32. return UnityEditor.TypeCache.GetTypesDerivedFrom(type);
  33. #else
  34. return GetAllAssemblyTypes().Where(t => t.IsSubclassOf(type));
  35. #endif
  36. }
  37. static IEnumerable<Type> m_AssemblyTypes;
  38. public static IEnumerable<Type> GetAllAssemblyTypes()
  39. {
  40. if (m_AssemblyTypes == null)
  41. {
  42. m_AssemblyTypes = AppDomain.CurrentDomain.GetAssemblies()
  43. .SelectMany(t =>
  44. {
  45. var innerTypes = new Type[0];
  46. try
  47. {
  48. innerTypes = t.GetTypes();
  49. }
  50. catch { }
  51. return innerTypes;
  52. });
  53. }
  54. return m_AssemblyTypes;
  55. }
  56. public static T GetAttribute<T>(this Type type, bool check = true) where T : Attribute
  57. {
  58. if (type.IsDefined(typeof(T), false))
  59. return (T) type.GetCustomAttributes(typeof(T), false) [0];
  60. else
  61. {
  62. if (check)
  63. Assert.IsTrue(false, "Attribute not found:" + type.Name);
  64. return null;
  65. }
  66. }
  67. public static T GetAttribute<T>(this MemberInfo type, bool check = true) where T : Attribute
  68. {
  69. if (type.IsDefined(typeof(T), false))
  70. return (T) type.GetCustomAttributes(typeof(T), false) [0];
  71. else
  72. {
  73. if (check)
  74. Assert.IsTrue(false, "Attribute not found:" + type.Name);
  75. return null;
  76. }
  77. }
  78. }
  79. }