|
| 1 | +#if AOT |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Python.Runtime |
| 9 | +{ |
| 10 | + public static class DynamicGenericHelper |
| 11 | + { |
| 12 | + private static HashSet<Type> _genericTypes = new HashSet<Type>(); |
| 13 | + |
| 14 | + [Conditional("AOT")] |
| 15 | + public static void RecordDynamicType(Type type) |
| 16 | + { |
| 17 | + if (_genericTypes.Contains(type)) |
| 18 | + { |
| 19 | + return; |
| 20 | + } |
| 21 | + if (!type.IsGenericType) |
| 22 | + { |
| 23 | + return; |
| 24 | + } |
| 25 | + var genericArgs = type.GetGenericArguments(); |
| 26 | + bool needRecord = false; |
| 27 | + foreach (var item in genericArgs) |
| 28 | + { |
| 29 | + if (item.IsValueType) |
| 30 | + { |
| 31 | + needRecord = true; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + if (!needRecord) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + _genericTypes.Add(type); |
| 40 | + } |
| 41 | + |
| 42 | + public static IEnumerable<string> GetAllTypeNames() |
| 43 | + { |
| 44 | + return _genericTypes |
| 45 | + .Select(T => GetDeclaringName(T)) |
| 46 | + .Distinct() |
| 47 | + .OrderBy(T => T.Substring(0, T.IndexOf("<"))); |
| 48 | + } |
| 49 | + |
| 50 | + private static string GetDeclaringName(Type type) |
| 51 | + { |
| 52 | + string name = type.FullName; |
| 53 | + var args = type.GetGenericArguments(); |
| 54 | + var declArgs = new string[args.Length]; |
| 55 | + string objName = typeof(object).FullName; |
| 56 | + for (int i = 0; i < args.Length; i++) |
| 57 | + { |
| 58 | + declArgs[i] = args[i].IsValueType ? args[i].FullName : objName; |
| 59 | + } |
| 60 | + string clsName = name.Substring(0, name.IndexOf('`')); |
| 61 | + return $"{clsName}<{string.Join(", ", declArgs)}>"; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +#endif |
0 commit comments