@@ -436,6 +436,7 @@ private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuild
436
436
il . Emit ( OpCodes . Ldloc_0 ) ;
437
437
438
438
il . Emit ( OpCodes . Ldtoken , method ) ;
439
+ il . Emit ( OpCodes . Ldtoken , method . DeclaringType ) ;
439
440
#pragma warning disable CS0618 // PythonDerivedType is for internal use only
440
441
if ( method . ReturnType == typeof ( void ) )
441
442
{
@@ -505,6 +506,7 @@ private static void AddPythonMethod(string methodName, PyObject func, TypeBuilde
505
506
506
507
il . DeclareLocal ( typeof ( object [ ] ) ) ;
507
508
il . DeclareLocal ( typeof ( RuntimeMethodHandle ) ) ;
509
+ il . DeclareLocal ( typeof ( RuntimeTypeHandle ) ) ;
508
510
509
511
// this
510
512
il . Emit ( OpCodes . Ldarg_0 ) ;
@@ -546,6 +548,11 @@ private static void AddPythonMethod(string methodName, PyObject func, TypeBuilde
546
548
il . Emit ( OpCodes . Ldloca_S , 1 ) ;
547
549
il . Emit ( OpCodes . Initobj , typeof ( RuntimeMethodHandle ) ) ;
548
550
il . Emit ( OpCodes . Ldloc_1 ) ;
551
+
552
+ // type handle is also not required
553
+ il . Emit ( OpCodes . Ldloca_S , 2 ) ;
554
+ il . Emit ( OpCodes . Initobj , typeof ( RuntimeTypeHandle ) ) ;
555
+ il . Emit ( OpCodes . Ldloc_2 ) ;
549
556
#pragma warning disable CS0618 // PythonDerivedType is for internal use only
550
557
551
558
// invoke the method
@@ -698,7 +705,7 @@ public class PythonDerivedType
698
705
/// class) it calls it, otherwise it calls the base method.
699
706
/// </summary>
700
707
public static T ? InvokeMethod < T > ( IPythonDerivedType obj , string methodName , string origMethodName ,
701
- object [ ] args , RuntimeMethodHandle methodHandle )
708
+ object [ ] args , RuntimeMethodHandle methodHandle , RuntimeTypeHandle declaringTypeHandle )
702
709
{
703
710
var self = GetPyObj ( obj ) ;
704
711
@@ -724,7 +731,10 @@ public class PythonDerivedType
724
731
}
725
732
726
733
PyObject py_result = method . Invoke ( pyargs ) ;
727
- PyTuple ? result_tuple = MarshalByRefsBack ( args , methodHandle , py_result , outsOffset : 1 ) ;
734
+ var clrMethod = methodHandle != default
735
+ ? MethodBase . GetMethodFromHandle ( methodHandle , declaringTypeHandle )
736
+ : null ;
737
+ PyTuple ? result_tuple = MarshalByRefsBack ( args , clrMethod , py_result , outsOffset : 1 ) ;
728
738
return result_tuple is not null
729
739
? result_tuple [ 0 ] . As < T > ( )
730
740
: py_result . As < T > ( ) ;
@@ -754,7 +764,7 @@ public class PythonDerivedType
754
764
}
755
765
756
766
public static void InvokeMethodVoid ( IPythonDerivedType obj , string methodName , string origMethodName ,
757
- object ? [ ] args , RuntimeMethodHandle methodHandle )
767
+ object ? [ ] args , RuntimeMethodHandle methodHandle , RuntimeTypeHandle declaringTypeHandle )
758
768
{
759
769
var self = GetPyObj ( obj ) ;
760
770
if ( null != self . Ref )
@@ -779,7 +789,10 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
779
789
}
780
790
781
791
PyObject py_result = method . Invoke ( pyargs ) ;
782
- MarshalByRefsBack ( args , methodHandle , py_result , outsOffset : 0 ) ;
792
+ var clrMethod = methodHandle != default
793
+ ? MethodBase . GetMethodFromHandle ( methodHandle , declaringTypeHandle )
794
+ : null ;
795
+ MarshalByRefsBack ( args , clrMethod , py_result , outsOffset : 0 ) ;
783
796
return ;
784
797
}
785
798
}
@@ -811,12 +824,11 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
811
824
/// as a tuple of new values for those arguments, and updates corresponding
812
825
/// elements of <paramref name="args"/> array.
813
826
/// </summary>
814
- private static PyTuple ? MarshalByRefsBack ( object ? [ ] args , RuntimeMethodHandle methodHandle , PyObject pyResult , int outsOffset )
827
+ private static PyTuple ? MarshalByRefsBack ( object ? [ ] args , MethodBase ? method , PyObject pyResult , int outsOffset )
815
828
{
816
- if ( methodHandle == default ) return null ;
829
+ if ( method is null ) return null ;
817
830
818
- var originalMethod = MethodBase . GetMethodFromHandle ( methodHandle ) ;
819
- var parameters = originalMethod . GetParameters ( ) ;
831
+ var parameters = method . GetParameters ( ) ;
820
832
PyTuple ? outs = null ;
821
833
int byrefIndex = 0 ;
822
834
for ( int i = 0 ; i < parameters . Length ; ++ i )
0 commit comments