使用pythonnet从python调用C#代码
下面是C代码。如何使用pythonnet从Python调用NonGenericClass中的GenericMethod()使用pythonnet从python调用C#代码,c#,python,interop,python.net,C#,Python,Interop,Python.net,下面是C代码。如何使用pythonnet从Python调用NonGenericClass中的GenericMethod() namespace CSharpTestCode { public interface Person { } public class Employee : Person { } public class TempGenericClass<T> { } public class
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;
}
}
}
我得到的错误:
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
[3]中的:NonGenericClass.GenericMethod[Person](TempGenericClass[Person]())
[4]中的NonGenericClass.GenericMethod[Employee](TempGenericClass[Employee]())
[5]中的NonGenericClass.GenericMethod[Person](TempGenericClass[Employee]())
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 NonGenericClass.GenericMethod[Person](TempGenericClass[Employee]())
TypeError:没有与给定参数匹配的方法
[6]中的NonGenericClass.GenericMethod[Employee](TempGenericClass[Person]())
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 NonGenericClass.GenericMethod[Employee](TempGenericClass[Person]())
TypeError:没有与给定参数匹配的方法
您是指嵌入式IronPython解释器吗?否则你不能直接调用它,你必须以某种方式(很多不同的方式)公开它。我正在使用pythonnet。我正在尝试将此代码构建为类库,python将通过访问此代码。dllI不知道调用此方法的python语法,因为它在方法名称和方法参数中都有泛型类型参数。此相关问题可能会帮助您:@bavaza我已经讨论了此问题。但这不是我所需要的
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