2017-07-18 8 views
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) 
+0

あなたが埋め込まれたIronPythonのインタプリタを意味するのですか?それ以外の場合は、直接呼び出すことはできません、何とかそれを公開する必要があります(多くの異なる方法) – UnholySheep

+0

私はpythonnetを使用しています。私はクラスライブラリとしてこのコードをビルドしようとしています。そして、Pythonは.dllを通してこのコードにアクセスします。 – Developer

+0

メソッド名とメソッド引数の両方にジェネリック型のパラメータを持っているので、このメソッドを呼び出すpython構文はわかりません – Developer

答えて

1

が、私はそのpythonnetがないのこの悪い例でも、CPythonのインタプリタをクラッシュすることになって認める必要があります:私が得た

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

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

エラーメソッドの引数が無効な汎用呼び出し。ここで

が正しく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

関連する問題