c# - 适用于 .NET 的 Python 中的可空类型 (C#)

标签 c# python .net nullable python.net

我目前正在使用 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/

相关文章:

python - 在 matplotlib 中绘制来自 librosa 的音频

c# - 如何从javascript返回一个数组? (执行脚本)

c# - 如何使用 C# 在 SQL Server 2008 中插入多个列表?

c# - 是什么导致我的 c# WinForms 应用程序卡住/挂起?

Python:在两个 python 安装之间共享 python 站点包库

python - pycurl 失败但 curl(来自 bash)在 ubuntu 中工作

.net - 使用监视器类

c# - 更新 net5.0 .csproj 到 net6.0

c# - 在 Asp.net Core 中使用 Ajax 验证表单

C# : Using hashtables to store two of the same value. 这可能吗?