3
次は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コード:
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)
あなたが埋め込まれたIronPythonのインタプリタを意味するのですか?それ以外の場合は、直接呼び出すことはできません、何とかそれを公開する必要があります(多くの異なる方法) – UnholySheep
私はpythonnetを使用しています。私はクラスライブラリとしてこのコードをビルドしようとしています。そして、Pythonは.dllを通してこのコードにアクセスします。 – Developer
メソッド名とメソッド引数の両方にジェネリック型のパラメータを持っているので、このメソッドを呼び出すpython構文はわかりません – Developer