18 using System.Collections.Generic;
20 using System.Reflection;
37 if (!typeof(TInterface).IsInterface)
39 throw new ArgumentException(
40 $
"{nameof(PythonWrapper)}.{nameof(ValidateImplementationOf)}(): {Messages.PythonWrapper.ExpectedInterfaceTypeParameter}");
43 var missingMembers =
new List<string>();
44 var members = typeof(TInterface).GetMembers(BindingFlags.Public | BindingFlags.Instance);
47 foreach (var member
in members)
49 if ((member is not MethodInfo method || !method.IsSpecialName) &&
50 !model.HasAttr(member.Name) && !model.HasAttr(member.Name.ToSnakeCase()))
52 missingMembers.Add(member.Name);
56 if (missingMembers.Any())
58 throw new NotImplementedException(
59 Messages.
PythonWrapper.InterfaceNotFullyImplemented(typeof(TInterface).Name, model.GetPythonType().Name, missingMembers));
73 public static T
InvokeMethod<T>(
this PyObject model,
string methodName, params
object[] args)
75 using var _ = Py.GIL();
76 return InvokeMethodImpl(model, methodName, args).GetAndDispose<T>();
85 public static void InvokeMethod(
this PyObject model,
string methodName, params
object[] args)
87 InvokeMethodImpl(model, methodName, args);
96 public static T
Invoke<T>(
this PyObject method, params
object[] args)
98 using var _ = Py.GIL();
99 return InvokeMethodImpl(method, args).GetAndDispose<T>();
107 public static PyObject
Invoke(
this PyObject method, params
object[] args)
109 return InvokeMethodImpl(method, args);
112 private static PyObject InvokeMethodImpl(PyObject model,
string methodName, params
object[] args)
114 using var _ = Py.GIL();
115 PyObject method = model.GetMethod(methodName);
116 return InvokeMethodImpl(method, args);
119 private static PyObject InvokeMethodImpl(PyObject method, params
object[] args)
121 using var _ = Py.GIL();
122 return method.Invoke(args.Select(arg => arg.ToPython()).ToArray());