以下是 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的 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]())
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
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'. …Run Code Online (Sandbox Code Playgroud)