使用pythonnet从python调用C#代码
问题描述:
以下是C#代码。我如何使用pythonnet从Python中的NonGenericClass内部调用GenericMethod()?使用pythonnet从python调用C#代码
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
+1
它的工作原理!非常感谢 – Developer
你的意思是一个嵌入式IronPython的解释?否则,你不能直接调用它,你必须以某种方式公开它(许多不同的方式) – UnholySheep
我正在使用pythonnet。我试图构建这个代码作为类库和python将通过.dll – Developer
访问此代码我不知道python语法来调用此方法,因为它具有方法名称和方法参数中的泛型类型参数 – Developer