Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14d0003

Browse files
committed
Record dynamic delegate binder
1 parent db6c99a commit 14d0003

File tree

6 files changed

+287
-220
lines changed

6 files changed

+287
-220
lines changed

src/runtime/classmanager.cs

+3
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ private static ManagedType GetDelegateProperty(PropertyInfo pi)
423423
return null;
424424
}
425425
ob = (ManagedType)Activator.CreateInstance(impType, pi);
426+
#if AOT
427+
DynamicGenericHelper.RecordDynamicType(impType);
428+
#endif
426429
return ob;
427430
}
428431

src/runtime/dynamicgenerichelper.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)