我目前正在使用 Python(适用于 .NET)开发一个从 C# .dll 调用函数的项目。在一个 C# 函数中有一个可为空的类型(double?)参数,显然我必须编辑 python 源代码(Creating a C# Nullable Int32 within Python (using Python.NET) to call a C# method with an optional int argument),我不想这样做,以防其他人会使用它,我也不想更新 C# 代码以处理这种转换情况。
目前,我尝试了以下方法:
nullable_double = System.Nullable[System.Double]()
func(nullable_double)
这给了我以下错误:
ArgumentException: Object of type 'System.RuntimeType' cannot be converted to type 'System.Nullable`1[System.Double]'.
我也试过只传递“无”,但效果不佳。无论如何,我可以在不更改 python 源代码或 c# dll 的情况下执行此操作吗?
最佳答案
请检查以下是否适用于您的func(nullable_double)
:
import clr
import System
nullable_double = System.Nullable[System.Double](0) # just providing a number for initialization
print(nullable_double) # prints: 0
print(type(nullable_double)) # prints: <class 'System.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'>
关于c# - 适用于 .NET 的 Python 中的可空类型 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39959889/