Extension.Type.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace TBL.CSharp.Base
  7. {
  8. public static partial class Extension
  9. {
  10. /// <summary>
  11. /// 获取可编译的类型名
  12. /// </summary>
  13. public static string GetCompilableName(this Type type, bool isFullName = false)
  14. {
  15. var name = isFullName ? type.FullName : type.Name;
  16. Debug.Assert(!string.IsNullOrEmpty(name));
  17. if (type.IsGenericType)
  18. {
  19. var strings = name.Split('`');
  20. var args = type.GenericTypeArguments;
  21. var sb = new StringBuilder();
  22. Debug.Assert(strings.Length > 1);
  23. for (var i = 0; i < args.Length; i++)
  24. {
  25. if (i != 0)
  26. sb.Append(", ");
  27. sb.Append(GetCompilableName(args[i], true));
  28. }
  29. name = $"{strings[0]}<{sb}>";
  30. }
  31. name = name.Replace('+', '.');
  32. return name;
  33. }
  34. /// <summary>
  35. /// 获取程序集列表
  36. /// </summary>
  37. public static IEnumerable<Assembly> GetAssemblies()
  38. {
  39. return AppDomain.CurrentDomain.GetAssemblies();
  40. }
  41. }
  42. }