gpt4 book ai didi

c# - 使用 pythonnet 从 python 调用 C# 代码

转载 作者:行者123 更新时间:2023-11-28 18:20:52 73 4
gpt4 key购买 nike

以下是 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 代码:

import clr
clr.AddReference(r'\Documents\visual studio 2015\Projects\SamplePythonApp\CSharpTestCode\bin\Debug\CSharpTestCode.dll')
from CSharpTestCode import *

genericMethod = NonGenericClass.GenericMethod(TempGenericClass[Employee]())

我得到的错误:

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)

最佳答案

我应该承认,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

关于c# - 使用 pythonnet 从 python 调用 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170180/

73 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com