我正在将一些代码从Ironpython移植到Python.NET。 我今天在Git(python 2.76)上运行最新的Python.NET,在Ubuntu 14.04的Mono上运行。 到目前为止一直很好,直到我遇到问题,试图明确说明一个通用参数。
我正在使用NHibernate数据库配置代码,我在尝试在C#类中显式声明泛型参数(NHSearch)时遇到问题,因此C#强类型语法使其可以推断 来自NHSearch.Instance。
python.NET中的违规行是:
db_adapter = DatabaseAdapter.Configure.DatabaseConfiguration(conn_sql).SearchProvider[NHSearch](NHSearch.Instance)
这适用于Ironpython,但在python.NET中,我收到此错误:
Unhandled Exception:
System.ArgumentException: GCHandle value cannot be zero
at System.Runtime.InteropServices.GCHandle.op_Explicit (IntPtr value)
[0x00000] in <filename unknown>:0
at Python.Runtime.ManagedType.GetManagedObject (IntPtr ob) [0x00000] in
<filename unknown>:0
at Python.Runtime.Runtime.PythonArgsToTypeArray (IntPtr arg, Boolean
mangleObjects) [0x00000] in <filename unknown>:0
at Python.Runtime.MethodBinding.tp_call (IntPtr ob, IntPtr args, IntPtr
kw) [0x00000] in <filename unknown>:0
at (wrapper native-to-managed) Python.Runtime.MethodBinding:tp_call
(intptr,intptr,intptr)
at (wrapper managed-to-native) Python.Runtime.Runtime:Py_Main
(int,string[])
at Python.Runtime.PythonConsole.Main (System.String[] args) [0x00000] in
<filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: GCHandle value
cannot be zero
at System.Runtime.InteropServices.GCHandle.op_Explicit (IntPtr value)
[0x00000] in <filename unknown>:0
at Python.Runtime.ManagedType.GetManagedObject (IntPtr ob) [0x00000] in
<filename unknown>:0
at Python.Runtime.Runtime.PythonArgsToTypeArray (IntPtr arg, Boolean
mangleObjects) [0x00000] in <filename unknown>:0
at Python.Runtime.MethodBinding.tp_call (IntPtr ob, IntPtr args, IntPtr
kw) [0x00000] in <filename unknown>:0
at (wrapper native-to-managed) Python.Runtime.MethodBinding:tp_call
(intptr,intptr,intptr)
at (wrapper managed-to-native) Python.Runtime.Runtime:Py_Main
(int,string[])
at Python.Runtime.PythonConsole.Main (System.String[] args) [0x00000] in
<filename unknown>:0
不确定如何/在何处开始调试或获得更有意义的回溯。任何提示都会有所帮助! 如果需要的话,我很乐意提供更多细节,但我不是 确定在这个阶段会有什么帮助,所以让我知道。
------------------------------------- EDIT --------- ----------------------------
我想提供更多信息...... c#中的NHSearch类不完全属于NHibernate。
以下是它的样子:
public class NHSearch : ISearchProvider
{
readonly DatabaseAdapter adapter;
public static NHSearch Instance(DatabaseAdapter adapter)
{
return new NHSearch(adapter);
}
NHSearch(DatabaseAdapter adapter)
{
this.adapter = adapter;
}
以下是DatabaseAdapter类的示例:
public class DatabaseAdapterBuilder : IConfigurationBuilder<DatabaseAdapter>
{
readonly DatabaseAdapter adapter;
public DatabaseAdapterBuilder(DatabaseAdapter adapter)
{
this.adapter = adapter;
}
public DatabaseAdapterBuilder DatabaseConfiguration(IPersistenceConfigurer config)
{
adapter.DatabaseConfiguration = config;
return this;
}
public DatabaseAdapterBuilder AddConvention(IConvention convention)
{
if (adapter.DatabaseConventions == null)
adapter.DatabaseConventions = new List<IConvention>();
adapter.DatabaseConventions.Add(convention);
return this;
}
public DatabaseAdapterBuilder SearchProvider<T>(Func<DatabaseAdapter, T> createProvider) where T : ISearchProvider
{
adapter.SearchProvider = createProvider(adapter);
return this;
}
public DatabaseAdapter Create()
{
return adapter;
}
}