1
- using System ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Runtime . InteropServices ;
3
4
4
5
namespace Python . Runtime
5
6
{
6
7
internal class CLRObject : ManagedType
7
8
{
8
9
internal object inst ;
9
-
10
+ internal static Dictionary < object , CLRObject > ObjDict { get ; private set ; } = new Dictionary < object , CLRObject > ( ) ;
11
+
10
12
internal CLRObject ( object ob , IntPtr tp )
11
13
{
12
14
IntPtr py = Runtime . PyType_GenericAlloc ( tp , 0 ) ;
@@ -37,7 +39,15 @@ internal CLRObject(object ob, IntPtr tp)
37
39
38
40
internal static CLRObject GetInstance ( object ob , IntPtr pyType )
39
41
{
40
- return new CLRObject ( ob , pyType ) ;
42
+ CLRObject clrObj ;
43
+ if ( ObjDict . TryGetValue ( ob , out clrObj ) )
44
+ {
45
+ Runtime . XIncref ( clrObj . pyHandle ) ;
46
+ return clrObj ;
47
+ }
48
+ clrObj = new CLRObject ( ob , pyType ) ;
49
+ ObjDict . Add ( ob , clrObj ) ;
50
+ return clrObj ;
41
51
}
42
52
43
53
@@ -57,6 +67,12 @@ internal static IntPtr GetInstHandle(object ob, IntPtr pyType)
57
67
58
68
internal static IntPtr GetInstHandle ( object ob , Type type )
59
69
{
70
+ CLRObject clrObj ;
71
+ if ( ObjDict . TryGetValue ( ob , out clrObj ) )
72
+ {
73
+ Runtime . XIncref ( clrObj . pyHandle ) ;
74
+ return clrObj . pyHandle ;
75
+ }
60
76
ClassBase cc = ClassManager . GetClass ( type ) ;
61
77
CLRObject co = GetInstance ( ob , cc . tpHandle ) ;
62
78
return co . pyHandle ;
0 commit comments