使用python从python调用C代码

2024-09-23 10:33:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是C代码。如何使用pythonnet在Python的NonGenericClass中调用GenericMethod()?在

namespace CSharpTestCode
{
    public interface Person
    {
    }

    public class Employee : Person
    {
    }

    public class TempGenericClass<T>
    {
    }

    public class NonGenericClass
    {
        public static T GenericMethod<T>(TempGenericClass<T> tempGeneric) where T : class, Person
        {
            return null;
        }
    }
}

我尝试的Python代码:

^{pr2}$

我得到的错误:

Unhandled Exception: System.ArgumentException: GenericArguments[0], 'CSharpTestCode.TempGenericClass`1[CSharpTestCode.Employee]', on 'T GenericMethod[T](CSharpTestCode.TempGenericClass`1[T])' violates the constraint of type 'T'. ---> System.Security.VerificationException: Method CSharpTestCode.NonGenericClass.GenericMethod: type argument 'CSharpTestCode.TempGenericClass`1[CSharpTestCode.Employee]' violates the constraint of type parameter 'T'.
   at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at Python.Runtime.MethodBinder.MatchParameters(MethodInfo[] mi, Type[] tp)
   at Python.Runtime.MethodBinder.Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
   at Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
   at Python.Runtime.MethodObject.Invoke(IntPtr target, IntPtr args, IntPtr kw, MethodBase info)
   at Python.Runtime.MethodBinding.tp_call(IntPtr ob, IntPtr args, IntPtr kw)

Tags: employeeargspublicsystematclassruntimeperson
1条回答
网友
1楼 · 发布于 2024-09-23 10:33:38

我应该承认,pythonnet不应该使CPython解释器崩溃,即使在这个错误的例子中,带有无效参数的泛型调用也是如此。在

下面是如何正确地使用pythonnet进行泛型调用,请注意传递错误的类型是如何正确失败的:

In [3]: NonGenericClass.GenericMethod[Person](TempGenericClass[Person]())


In [4]: NonGenericClass.GenericMethod[Employee](TempGenericClass[Employee]())


In [5]: NonGenericClass.GenericMethod[Person](TempGenericClass[Employee]())
                                     -
TypeError                                 Traceback (most recent call last)
<ipython-input-5-d13751f7586f> in <module>()
  > 1 NonGenericClass.GenericMethod[Person](TempGenericClass[Employee]())

TypeError: No method matches given arguments


In [6]: NonGenericClass.GenericMethod[Employee](TempGenericClass[Person]())
                                     -
TypeError                                 Traceback (most recent call last)
<ipython-input-6-04c3c0db6c6b> in <module>()
  > 1 NonGenericClass.GenericMethod[Employee](TempGenericClass[Person]())

TypeError: No method matches given arguments

相关问题 更多 >